Java Program to check string is palindrome or not


In this program, You will learn how to check string is palindrome or not in Java.


Some list of palindrome string is: dad madam, level


Example: How to check string is palindrome or not in Java

Run it Now
import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        Scanner s = new Scanner(System.in);

        String str;
        int l = 0, i, f = 1;

        System.out.print("Enter a String :");
        str = s.nextLine();
        char a[] = str.toCharArray();

        for (i = 0; i < a.length; i++) {
            l++;
        }

        l = l - 1;
        i = 0;

        while (i < l) {
            if (a[i] != a[l]) {
                f = 0;
                break;
            }
            l--;
            i++;
        }
        if (f == 1) {
            System.out.print("\nString is Palindrome :" + str);
        } else {
            System.out.print("\nString is Not Palindrome :" + str);
        }

    }
}

Output:

Enter a String :level
String is Palindrome :level