-2

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.

kaya3
  • 47,440
  • 4
  • 68
  • 97
Very good
  • 1
  • 1
  • 1
    Your tags don't say anything about your approach. You don't mention which programming language (hint: a LISP program will take a totally different approach than a C++ program than a Fortran program than a Javascript program). You don't say what you've done so far. You spam with unnecessary greeting and TIA phrases. – Marcus Müller Sep 19 '15 at 14:52
  • Convert the string toLowerCase. You could loop through the string and analyse each char at a time to see if it is not a space. Then, loop through with your palindrome logic, which is typically to loop twice; forwards and backwards through the string. If all char match, then it's a palindrome. – Peter Sep 19 '15 at 14:54
  • @Peter, if you're going to use a language that uses Latin script, upper case is a more dependable case-folding operation than lower, since "SS" to lower will always be "ss" and so won't match "ß". Of course, if you have an actual "Case Fold" method available, that's better still. – Jon Hanna Sep 19 '15 at 15:10

1 Answers1

-1
      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.");
thegauravmahawar
  • 2,802
  • 3
  • 14
  • 23