Java Program to Check if a Given String is Palindrome.

Program to Check if a Given String is Palindrome


import java.util.Scanner;


public class PalindromeString{

    public static void main(String[] arg
    {
        int i,j;

        //Ask for user input and store in a String 'Str'
        System.out.print("Enter String: ");

        Scanner sc =  new Scanner(System.in);
        String Str = sc.nextLine();

        //Store the last position of string in the variable 'last'
        
        int last = Str.length()-1;

  
        for(i=0,j=last ; i<j ; i++, j--)
        {

        //Comparing the first and last character of the String
          if(Str.charAt(i)!=Str.charAt(j))
            
                {
                System.out.println("Non-Panlindrome Sring");
                break;
                }
            
        }

        if(i==j)

                System.out.print("Palindrome String");
        
    }
}


Comments

Popular posts from this blog

Java Program to replace vowel with next immediate character alphabetically in a String.

Java Program to count and output the number of double letter sequences that exist in the string.

Java Program to find the minimum and maximum palindrome String in a Sentence