Checking a String is Palindrome or Not in JAVA

Palindrome: A word,phrase or a sentense which reads samae as backwards as forwards.
Example: madam,radar etc.


//This is the program to check whether a String is Palindrome or not by reversing string or using if else statement


public class PalindromeString
{
public static void main(String args[])
{
//Declaring and initializing a string
String str="madam"; 
String rev="";  //no space string
int n = str.length();  //To get length of string
for(int i=n-1; i>=0; i--)
{
     rev=rev+str.charAt(i);    
}

//to check whether a string is palindrome or not
if(rev.equals(str))
{
Sytsem.out.println(str+" is Palindrome.");
}
else
{
System.out.println(str+" is not Palidrome.");
}
}
}

Output:

madam is Palindrome.

1 Comments

Previous Post Next Post

Contact Form