A String is said to be a Palindrome String if after reversing it, it results in the original string.

Example: MADAM, EYE, RADAR, CIVIC, etc.

The basic logic for checking Palindrome String is to calculate its length and then reverse it using a loop construct by running a loop and extracting characters from (N-1)th position to 0th position. Store the same in another variable and the compare result with original string.

Code:

/* Program to enter a String and check if it is Palindrome or not.*/
import java.util.*;
class PalindromeString
{
public static void main()
{
Scanner inp=new Scanner(System.in);

System.out.print("\n Enter String: ");
String s=inp.nextLine();
String z="";
char c;
int i,k=s.length();

for(i=(k-1);i>=0;i--)
{
c=s.charAt(i);
z=z+c;
}

if(z.equals(s)==true)   //Comparing Both Strings
  System.out.println(s+" is a Palindrome String");
else
  System.out.println(s+" is not a Palindrome String");
  
}
}

Output:

Enter String: REFER
REFER is a Palindrome String

Report Error/ Suggestion

Related Posts:

[yuzo_views]











CopyRight © 2019

CopyRight © 2019