0

I am trying to create a method called "palindrome" that receives a String and returns a boolean value of true if the String is a Palindrome and false if it is not. A word is a palindrome if it reads the same forwards and backward. For example, the word level is a palindrome.

For Example as palindromes by considering the text that is obtained by removing all spaces and punctuation marks and converting all letters to their lower-case form:

Madam, I'm Adam ==> madamimadam

A man, a plan, a canal: Panama ==> amanaplanacanalpanama

I tried to work on my code that I used replaceAll(); and output as the last line of replacing all.

public static String palindrome(String n){

      char[] input = n.toCharArray();

    //for(int i=0; i<input.length; i++){ //looping reversing using input as in length minus 1 from end to start and counting using increment
      n.toLowerCase();
      String noSpaces = n.replaceAll(" ", ""); //I used this to get string 
      String remove = noSpaces.replaceAll(",","");
      String remove2 = remove.replaceAll(".","");
      String remove3 = remove2.replaceAll(":","");
      String remove4 = remove3.replaceAll("!", "");
      System.out.print(remove4);    
      //return n;  
    //}
    return n;
    }

Compiler/Output:

Input: Madam, I'm Adam

Output:

The output is showing nothing anything? What am I doing wrong?

  • Related: https://stackoverflow.com/questions/27688614, https://stackoverflow.com/questions/49416011, https://stackoverflow.com/questions/10827872 – tkruse Mar 03 '20 at 04:36
  • also note there is a bug, `n.toLowerCase();` does not actually change n. – tkruse Mar 03 '20 at 04:38

3 Answers3

0

The first parameter of Java's replaceAll() is regex. . in regex matches any character, so remove.replaceAll(".", "") effectively gives you an empty string.

Escape the period & you should be getting a string returned again.

String remove2 = remove.replaceAll("\\.","");
ItsPete
  • 2,363
  • 3
  • 27
  • 35
0

The problem is the dot essentially matches any character any number of times. So if you put remove.replaceAll(".","") all the characters gets replaced with nothing. You should escape the dot to specify the actual dot. Hope this helps.

n = n.toLowerCase();//you should assign the result to get lowercase characters
String noSpaces = n.replaceAll(" ", ""); //I used this to get string 
String remove = noSpaces.replaceAll(",","");
String remove2 = remove.replaceAll("\\.","");
String remove3 = remove2.replaceAll(":","");
String remove4 = remove3.replaceAll("!", "");
String remove5 = remove2.replaceAll("\'","");//you should also escape apostrophe
System.out.print(remove5);
sachin
  • 777
  • 5
  • 10
0

To remove all special characters, you can simply use remove.replaceAll("\\p{Punct}", "")

Also, you can simplify your code by chaining "removes" like this:

String output = n.toLowerCase().replaceAll(" ", "").replaceAll("\\p{Punct}", "");
System.out.println(output);

Now, this in itself doesn't return a palindrome nor it checks for it. To check for palindromes, you have to create a method that places pointers on both the head and tail ends of your string and compare the characters at both indices to see if they match. You will increment and decrement those pointers respectively until:

  1. The characters are not the same, or
  2. The value of the tail pointer is less than the head pointer.

For instance:

boolean isPalindrome(String word) {
    int headPtr = 0;
    int tailPtr = word.length() -1 ;

    while (headPtr < tailPtr) {
        if (word.charAt(headPtr) != word.charAt(tailPtr))
            return false;
        headPtr++;
        tailPtr--;
    }
    return true;
}

You can test with even and odd numbered strings to make sure the break condition is right. For example otto, hannah, 1234321, and even your long phrase. The finished implementation should look something like this:

public class PalindromeTest {

    static boolean isPalindrome(String word) {
      int headPtr = 0;
      int tailPtr = word.length() -1 ;

      while (headPtr < tailPtr) {
        if (word.charAt(headPtr) != word.charAt(tailPtr))
            return false;
        headPtr++;
        tailPtr--;
      }
      return true;
  }

  public static void main (String[] args) {

    String n = "A man, a plan, a canal: Panama";

    String output = n.toLowerCase().replaceAll(" ", "").replaceAll("\\p{Punct}", "");
    System.out.println(n + " is palindrome? " + isPalindrome(output));
  }
}
hfontanez
  • 5,774
  • 2
  • 25
  • 37