# Thread: Compare letters of 2 strings

1. Member
Join Date
Apr 2013
Posts
11
Rep Power
0

## 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:

Java Code:
``` for(int i=0; i<sequence1.length();i++){

if(sequence1.charAt(i)==sequence2.charAt(i)){

return 0;
}
else{
return 1;
}```

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,904
Rep Power
10

## 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

3. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

## 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.

4. Member
Join Date
Apr 2013
Posts
11
Rep Power
0

## Re: Compare letters of 2 strings

I want to know how many different words random strings have. I started writing the code above, but there is already this problem with ++.

5. Member
Join Date
Apr 2013
Posts
11
Rep Power
0

## 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).

6. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,904
Rep Power
10

## Re: Compare letters of 2 strings

There is no problem with the autoincrement operator. It is your coding logic which is incorrect.

Regards,
Jim

7. Member
Join Date
Apr 2013
Posts
11
Rep Power
0

## Re: Compare letters of 2 strings

Yes, you are right.

Now I tried:

Java 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.

8. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,904
Rep Power
10

## 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:

Java 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

9. Member
Join Date
Apr 2013
Posts
1
Rep Power
0

## Re: Compare letters of 2 strings

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

10. ## Re: Compare letters of 2 strings

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•