1

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++;
  }
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Jace
  • 75
  • 2
  • 6

2 Answers2

2

I'd remove all the special characters, split the string by whitespaces and check the list is "symmetrical":

private static boolean isSentancePalindrom(String sentence) {
    String[] words = sentence.replaceAll("[^a-zA-Z ]", "").split("\\s+");
    for (int i = 0; i < words.length / 2; ++i) {
        if (!words[i].equalsIgnoreCase(words[words.length - i - 1])) {
            return false;
        }
    }

    return true;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

I came up with this:

   public static boolean checkString(String str) {

    str = str.replaceAll("[,\\?\\.!]+", "").toUpperCase();

    String[] split = str.split(" ");
    String[] reverse = new String[split.length];

    System.arraycopy(split, 0, reverse, 0, split.length);

    List<String> listOfSring = Arrays.asList(split);
    List<String> reversListOfSring = Arrays.asList(reverse);

    Collections.reverse(reversListOfSring);
    return reversListOfSring.equals(listOfSring);   

}

I hope it would help!

Mustapha Belmokhtar
  • 1,231
  • 8
  • 21
  • You can omit the for-loop and just do `return listOfString.equals(reverseListOfString)`. Besides, you can omit the `arraycopy()` and initialize the second list exactly like you initialize the first one. – sinclair Nov 21 '17 at 19:35
  • @sinclar thanks for the tips, but it didn't work by doing the same initialization in both lists, because by reversing one the other gets reversed too ! – Mustapha Belmokhtar Nov 22 '17 at 00:16
  • Sorry I misunderstood, you were talking about initializing both arrays by using the split method, I got it, I will edit it once again – Mustapha Belmokhtar Nov 22 '17 at 01:13