break it up into chars and then compare it
ilovejava
Junior Poster in Training
77 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
use compareTo method of string to compare two string if it return 0 then they are equal else not equal.
Example:-
firstString.compareTo(secondString)==0 then equal.
rushikesh jadha
Junior Poster in Training
89 posts since Dec 2011
Reputation Points: 4
Solved Threads: 11
use compareTo method of string to compare two string if it return 0 then they are equal else not equal.
Example:-
firstString.compareTo(secondString)==0 then equal.
why do you always make things harder then need be?
if you want to check two String objects for equality, why not use the equals method?
but, you may have noticed, this is not what the OP is trying to do.
he is trying to compare a String object to the reverse of another String object.
I would go with ilovejava's answer here.
step 1. check if they have the same length: if not, it's impossible for them to be palindromes return false
--. all next steps only run if the first step didn't return false
step 2. convert (at least) one of your Strings to a char array, or use the charAt() method to get a similar behaviour of your code
compare the Strings char by char, one String forwards, the other one backwards. you find a combination that isn't equal: return false
--. if none of the above steps returned false:
step 3. by default: return true
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433