-1

Here's my Java code to find a palindrome of a string. It shows o/p as "Palindrome" even though the i/p that I've entered is not. Can anyone please help.

String a = sc.next();
char b[] = a.toCharArray();
char d[] = b;
int size = a.length();
int beg=0,end=size-1;

while(beg<=end)
{
    char temp = b[beg];
    b[beg] = b[end];
    b[end] = temp;
    beg++;
    end--;
}

 if(d.equals(b))
    {
     System.out.print("Palindrome");
    }
else
    System.out.print("Not a Palindrome");
solid.py
  • 2,782
  • 5
  • 23
  • 30
  • 5
    Does this answer your question? [Check string for palindrome](https://stackoverflow.com/questions/4138827/check-string-for-palindrome) – donquih0te Mar 07 '20 at 16:11

3 Answers3

1

There are a few problems with your implementation. First of all -

while(beg<=end)
{
    char temp = b[beg];
    b[beg] = b[end];
    b[end] = temp;
    beg++;
    end--;
}

Debug carefully and see do you really have d[] as the reverse of b[] at the end of this while loop

Secondly,

 if(d.equals(b))
    {
     System.out.print("Palindrome");
    }

This is not the correct way to compare if all the array elements are same in both of the array. If you look into the implementation or try out with some sample array you will be able to see it yourself.

To check palindrome, very simple approach is to reverse the string using StringBuilder and check if it's equal to the original string -

    Scanner sc = new Scanner(System.in);
    String a = sc.next();
    String aRev = new StringBuilder(a).reverse().toString();


    if (a.equals(aRev)) {
        System.out.print("Palindrome");
    } else {
        System.out.print("Not a Palindrome");
    }

Another better approach is to run a loop from beginning to middle of the string and keep an index from end to middle. Then check both forward index and backward index.

    Scanner sc = new Scanner(System.in);
    String a = sc.next();

    boolean palindrome = true;

    for (int i = 0; i < a.length() / 2; i++) {
        if (a.charAt(i) != a.charAt(a.length() - i - 1)) {
            palindrome = false;
            break;
        }
    }

    System.out.println(palindrome ? "Palindrome" : "Not Palindrome");
solid.py
  • 2,782
  • 5
  • 23
  • 30
shakhawat
  • 2,639
  • 1
  • 20
  • 36
0

Try this code, it works as expected.

public class Main {
    public static boolean isPalindrome(String str) {
        int i = 0, j = str.length() - 1;

        // The following condition checks if the decreasing index 
        // is larger than the increasing one, 
        // which happens at the middle of the string.
        // This is done, because there is no need to iterate over,
        // the entire string.
        while(j > i){ // Non-matching character found, return early.
            if(str.charAt(i) != str.charAt(j)){
                return false;
            }
            j--; // Ending index decreases.
            i++; // Starting index increases.
        }
        // If the loop finishes, without returning all characters of the string match.
        return true;
    }
    public static void main(String[] args) {
        String string = "Non-palindrome";
        String palindrome = "WoW";
        System.out.println("String " + string + " is palindrome: " + isPalindrome(string));
        System.out.println("String " + palindrome + " is palindrome: " + isPalindrome(palindrome));
    }
}

When run, the above code outputs:

String Non-palindrome is palindrome: false
String WoW is palindrome: true
solid.py
  • 2,782
  • 5
  • 23
  • 30
0

With

char d[] = b;

you create the alias d for b. So, in essence, b and d refer to the same char-Array. There is no difference beween modifying b and modifying d. Therefore, the comparison d.equals(b) will always yield true.