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.

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("String is palindrome:" + str);
        } else {
            System.out.print("String is not palindrome:" + str);
        }

    }
}

Output:

Enter a string:level
String is palindrome:level