How to check if a string is a palindrome ?

How to check if a string is a palindrome ?

Program
if the string is similar from the starting as well as the ending, then we can say that the string is a palindrome.
For example. madam, radar, etc.
import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
        int flag=0,low,high;
        String s;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter String :");
        s=sc.nextLine();
        low = 0;
        high = s.length() - 1;
        while (low < high) 
        {
            if (s.charAt(low) != s.charAt(high)) 
            {
                flag=1;
                break;
            }
            low++; 
            high--; 
        }
        if(flag==0)
        {
            System.out.println("is a palindrome");
        }
        else
        {
            System.out.println("not a palindrome");
        }	
    }
}

Output:

Enter String : sateesh
not a palindrome


Enter String :
radar
is a palindrome





More Questions


17 . How to count the occurrence of the given character in a string?
18 . How to check if a string is a palindrome?
19 . How to count the number of words in a given string sentence?
20 . How to reverse the words from the given string sentence?
21 . How to swap two strings without using the third variable?