Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Compare strings of different lengths without using strcmp?

Subject: Compare strings of different lengths without using strcmp?

From: NouveauIX

Date: 24 Sep, 2009 20:09:00

Message: 1 of 6

I'm trying to compare two strings with if statements without using strcmp.

function log=myStrCmpI(char1,char2)
c1=lower(char1);
c2=lower(char2);
if ?
    log=true;
else log=false;
end

I tried taking the sum of c1-c2 but that doesn't work for all cases, such as if the strings are the same but have a couple letters switched around.

Subject: Compare strings of different lengths without using strcmp?

From: dpb

Date: 24 Sep, 2009 20:10:15

Message: 2 of 6

NouveauIX wrote:
> I'm trying to compare two strings with if statements without using strcmp.
>
> function log=myStrCmpI(char1,char2)
> c1=lower(char1);
> c2=lower(char2);
> if ?
> log=true;
...
Well, one has to start w/ a clear definition of what the criterion for
equality would be. If the two aren't the same length, then they
obviously can't be identical so what would "true" be indicating, anyway?

--

Subject: Compare strings of different lengths without using strcmp?

From: dpb

Date: 24 Sep, 2009 20:16:19

Message: 3 of 6

dpb wrote:
...
> Well, one has to start w/ a clear definition of what the criterion for
> equality would be. If the two aren't the same length, then they
> obviously can't be identical so what would "true" be indicating, anyway?

OBTW, if they are the same length,

flg = all(s2==s1);

If they're not, again, "Houston, we have a dimensions problem"...

--

Subject: Compare strings of different lengths without using strcmp?

From: NouveauIX

Date: 25 Sep, 2009 05:28:52

Message: 4 of 6

Oh I'm sorry. They need to be equal disregarding case. So 'banana' and 'BaNana' would be equal, as would 'purple' and 'purple'. Result is true if the strings are equal, false if not. I need to be able to enter strings of lengths that aren't equal, though, even if the result will be false.

Subject: Compare strings of different lengths without using strcmp?

From: Jan Simon

Date: 25 Sep, 2009 07:32:04

Message: 5 of 6

Dear NouveauIX!

> I'm trying to compare two strings with if statements without using strcmp.
>
> function log=myStrCmpI(char1,char2)
> c1=lower(char1);
> c2=lower(char2);
> if ?
> log=true;
> else log=false;
> end

  function Log = myStrCmpI(char1,char2)
  Log = (numel(char1) == numel(char2)) && ...
           all(lower(char1) == lower(char2));
  return;

1. The && operator performs short-circuiting: the 2nd part of the comparison is not evaluated, if the 1st is FALSE already. This is important.
2. I've renamed "log" to "Log" to reduce conflicts with Matlab's logarithm function. Although this is not an error, it is worth to avoid as many confusions as possible...

Finally: STRCMPI is really fast and does check the types of the inputs. This can be a tremendous help for debugging! In addition STRCMPI works for cell strings also and cares about CHAR arrays with more than 1 row.

Good luck, Jan

Subject: Compare strings of different lengths without using strcmp?

From: Loren Shure

Date: 25 Sep, 2009 17:41:06

Message: 6 of 6

In article
<1257816834.5891.1253856562126.JavaMail.root@gallium.mathforum.org>,
visualxd@gmail.com says...
> Oh I'm sorry. They need to be equal disregarding case. So 'banana' and 'BaNana' would be equal, as would 'purple' and 'purple'. Result is true if the strings are equal, false if not. I need to be able to enter strings of lengths that aren't equal, though, even if the result will be false.
>


Have you looked at strcmpi - which allows you to compare strings in a
case-insensitive manner.

--
Loren
http://blogs.mathworks.com/loren

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us