English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

String word = "What's up? Did Sara's mom fly on a 74-7?"

public String getPalindromes()
{
String result = "",y = "";
String AZ = "abcdefghijklmnopqrstuvwxyz1234567890";
String x = "`~!@#$%^&*()-_+?<>,./'";

for (int j = 0; j {
if (x.indexOf(word.charAt(j)) == -1)
y += word.charAt(j);
}
//System.out.println("\n" +y);

input = new Scanner(y);
while (input.hasNext())
{
String word = input.next();
String back = word.toLowerCase();

String temp = "";
for (int i = back.length()-1; i>=0; i--)
{
if (AZ.indexOf(back.charAt(i)) != -1)
temp = temp + back.charAt(i);
}


input = new Scanner(word);

//System.out.println(temp);

if (back.equalsIgnoreCase(temp))
result += temp + " ";
}

return result;

how do I get a result of Did Sara's mom a 74-7 instead of did saras mom a 747 which I have right now?

2006-10-29 05:00:39 · 3 answers · asked by ? 1 in Computers & Internet Programming & Design

I followed Gizmo's suggestion but it didn't work

2006-10-29 05:48:52 · update #1

3 answers

Comment out these lines, as you may want them later.

if (x.indexOf(word.charAt(j)) == -1)
y += word.charAt(j);
}

Change this line
String back = word.toLowerCase();
to
String back = new String(word);
So it is not in lower case

2006-10-29 06:16:39 · answer #1 · answered by Mark aka jack573 7 · 0 0

Well this code at the top:

or (int j = 0; j {
if (x.indexOf(word.charAt(j)) == -1)
y += word.charAt(j);
}

Has the purpose of stripping out non letter characters from the string you're going to compare. You would need to change your code so that when you are done, you add this original variable word to result, rather than the variable temp, so probably:

Change:

if (back.equalsIgnoreCase(temp))
result += temp + " ";
}

to

if (back.equalsIgnoreCase(temp))
result += word + " ";
}

2006-10-29 13:34:47 · answer #2 · answered by Gizmo L 4 · 0 0

Sounds like a homework program... the best way to learn is checking out reference books and researching exactly how Palindromes work, then create an algorithm .

That's the way I learned... had no one help, not even the profs.

2006-10-29 13:10:19 · answer #3 · answered by Josh R 2 · 0 0

fedest.com, questions and answers