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
Post a Comment