i'm writing a program that will recognize a palindrome when input by JOptionPane. it will recognize the phrase as palindrome if it is one and will tell me if the phrase is not a palindrome correctly. the problem is that i need to get the phrase to be recognized with spaces and punctuation which i haven't been able to do up to this point. my teacher gave me the ascii chart for upper case and lower case charts of conversion, but that doesn't help me with spaces and punctuation. i have looked up the charts for the other codes, but i don't know what to do how to implement them into the program. here is the code:
import javax.swing.JOptionPane;
import java.io.*;
class Prog7c{
publicstaticvoid main(String[] args){
//the declaration and JOptionPane and limit
String str = new String();
str = JOptionPane.showInputDialog("Please Enter In A Phrase.");
int limit = str.length()-1;
//printing statement
System.out.println("Forwards: " + str);
//the palindrome
boolean flag = true;
String strLower = str.toLowerCase();
String strNew = "";
//area in question below: the two for loops and the two char declarations
char c = 32;
char d = 47;
for(int k=0;k<=limit;k++){
if((strLower.charAt(k) >= 'a') && (strLower.charAt(k)<='z') && (strLower.charAt(k)>= c) && (strLower.charAt(k)<= d)){
strNew = strNew + strLower.charAt(k);
}
}
for(int j=0;j<=limit/2;j++){
if(strLower.charAt(j) != strLower.charAt(limit - j)){
flag = false;
}
}
if(flag){
System.out.println("\n\nThe Phrase is a Palindrome.");
}
else
System.out.println("\n\nThe Phrase is NOT a Palindrome.");
System.exit(0);
}
}
I like your idea of copying the string with spaces and punctuation removed. Take a look at the Character class, isLetter() method. See if that helps you decide which characters to keep and which to discard.
Then be sure you're checking your strNew when you test for the palindrome.