# Compare letters of 2 strings

Printable View

• 04-28-2013, 04:08 AM
reverton
Compare letters of 2 strings
I want to compare letters of 2 same size strings. If they are the same return different return 1 and if they are the same return zero. I just don't understand why i++ is not working:

Code:

``` for(int i=0; i<sequence1.length();i++){                                 if(sequence1.charAt(i)==sequence2.charAt(i)){                                                 return 0;                        }                 else{                         return 1;                        }```
• 04-28-2013, 04:13 AM
jim829
Re: Compare letters of 2 strings
It "is" working. The problem is that you are returning either a 0 or a 1 first time through. You only want to return 0 if all the characters are equal. But you can return a 1 the first time the characters are not equal.

Regards,
Jim
• 04-28-2013, 04:14 AM
pbrockway2
Re: Compare letters of 2 strings
What makes you suppose that the increment operator (++) is broken?

Consider posting runnable code and saying what you expect or intend to happen.
• 04-28-2013, 04:48 AM
reverton
Re: Compare letters of 2 strings
Thanks for your reply.

It is saying dead code.

I want to know how many different words random strings have. I started writing the code above, but there is already this problem with ++.
• 04-28-2013, 04:56 AM
reverton
Re: Compare letters of 2 strings
P.S.: The letters compared must be in same position. If string 1 = abcd and string 2 = abbd I want the result of 1 because there is 1 letter in same position that is different(third letter in both strings).
• 04-28-2013, 05:10 AM
jim829
Re: Compare letters of 2 strings
There is no problem with the autoincrement operator. It is your coding logic which is incorrect.

Regards,
Jim
• 04-28-2013, 05:30 AM
reverton
Re: Compare letters of 2 strings
Yes, you are right.

Now I tried:

Code:

``` int counter = 0;                     for(int i=0; i<sequence1.length();i++){                                 if(sequence1.charAt(i)!=sequence2.charAt(i)){                                                 counter++;                        }                 return counter;```
where sequence1 and sequence 2 are 2 strings of same size. And counter would be the number of different letters at same position in both strings. But I am still doing something wrong, because ++ in the loop still warns as dead code. Can you advise me what could be my mystake? Thanks.
• 04-28-2013, 05:41 AM
jim829
Re: Compare letters of 2 strings
The dead code is being caused by the fact that you are returning during the first iteration of the loop. So i++ never has a chance to increment. What you need to do is the following:

Code:

```for(int i=0; i<sequence1.length();i++){     if(sequence1.charAt(i)!=sequence2.charAt(i)){         return 1;  // characters not equal so strings not equal     } } return 0;  // all characters equal so strings are equal```
Regards,
Jim
• 04-28-2013, 08:20 AM
haowilliam
Re: Compare letters of 2 strings
length is a field ,not a method ,so you should code as "length" rather than "length()";
• 04-28-2013, 08:40 AM
JosAH
Re: Compare letters of 2 strings
Quote:

Originally Posted by haowilliam
length is a field ,not a method ,so you should code as "length" rather than "length()";

For arrays it's a field; for Strings (and other object types) it's a method.

kind regards,

Jos