I am trying to create a palindrome check. I am using StringBuilder and I found that the added spaces are quite complex.
EDIT: are there any other ways besides using .reverse ()? Thanks for the answers .: D
This code works when a word has no spaces:
public String palindrome (String anyString) {
StringBuilder sb = new StringBuilder();
for (int i = anyString.length()-1; i>=0; i--) {
sb.append(anyString.charAt(i));
}
String string2 = sb.toString();
return string2;
}
When the word entered has a space; it returns a string containing characters up to the first space.
eg:.
word = "not a palindrome"
palindrome(word) = "emordnilap"
expected = "emordnilap a ton"
I tried to insert
if (anyString.charAt(i) != ' ')
sb.append(anyString.charAt(i));
else
sb.append(' ');
in the middle of the code, but it does not work.
Thanks guys!
source
share