Search code examples
palindromespacesletter

Don't take capital letters/spaces in consideration


For our workgroup we've got to write a program that detects whether the given phrase is a "palindrome" (in other words, when one reverts the entire phrase, it still says the same). Now that does sound easy at first sight but the program must be able to completely ignore any spaces or capital letters.

This so happens to be something we have not yet been taught, so hereby. I don't ask for the entire solution, I would only like to know how to write something that does read strings but not any given spaces or capital letters.


Solution

  •       String original, reverse = "";
          Scanner in = new Scanner(System.in);
    
          System.out.println("Enter a string to check if it is a palindrome");
          original = in.nextLine();
          original = original.toLowerase();
    
          int length = original.length();
    
          for ( int i = length - 1; i >= 0; i-- )
             reverse = reverse + original.charAt(i);
    
          if (original.equals(reverse))
             System.out.println("Entered string is a palindrome.");
          else
             System.out.println("Entered string is not a palindrome.");