I am practising a string palindrome. Is my code in my for loop right?
public static void main (String args[])
{
String word = JOptionPane.showInputDialog("Input a String:");
String finalword = word.replaceAll(" ","").toLowerCase();
for (int x = word.length(); x >= word.length()-1; x--)
{
finalword.charAt(x);
}
if(word.equals(finalword))
{
JOptionPane.showMessageDialog(null, "Palindrome");
}
else
{
JOptionPane.showMessageDialog(null, "Not a Palindrome");
}
}
Thank you and best regards.
The actual string you need to work on is finalword as its length important to you.
For simplicity take two temp arrays like tempStart and tempLast, both has to be same length as (finalword.length /2), Dont worry about string has even or odd number of chars.
use for loop with two variables as follows.
if these both arrays (tempStart and tempLast) matches then its palindrome otherwise not.
I hope this will help you. If not let me know you need any more help while doing this.