I am trying to check if a sentence is the same forwards and backwards or a "sentence palindrome." The sentence "You can cage a swallow, can't you, but you can't swallow a cage, can you?" should return (True) as a palindrome. Ignore everything that is not a letter.
My problem: Not sure how to compare words specifically. This currently works for words checking if they are palindromes, but I need to figure out what to change to compare each word.
public static boolean isWordPalindrome(String input) {
Deque<Character> q = new LinkedList<>( );
Deque<Character> q2 = new LinkedList<>( );
Character letter; // One character from the input string
int mismatches = 0; // Number of spots that mismatched
int i; // Index for the input string
int x;
for (i = 0; i < input.length( ); i++)
{
letter = input.charAt(i); // read next character in the string
if (letter.toString().equals(',') || letter.toString().equals('"') || letter.toString().equals('?') || letter.toString().equals('!') || letter.toString().equals('.') || letter.toString().equals(' ')) {
//throwaway.add(letter); //ignore above chars and put in throwaway stack
}
if (Character.isLetter(letter)) // if letter put into q's
{
q.add(letter);
q2.addFirst(letter);
}
} // end of for loop
System.out.println("q: " + q);
System.out.println("q2:" + q2);
while (!q.isEmpty( ))
{
if (!Objects.equals(q.remove(), q2.remove()))
mismatches++;
}