Java Program to check whether the string is Palindrome or not

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 
package com.spy.javaforlearn;
 
import java.util.Scanner;
 
public class Pelindrom 
{
 
	public static void main(String[] args) 
	{
 
		//Defining scanner class to take input from user
		Scanner scn=new Scanner(System.in);
 
		System.out.println("Enter a String");
 
		//Taking input of type string from user
		String s=scn.nextLine();
 
 
		// String object to store Reverse String
		String rev="";
 
 
		// calculating length of input String
		int length=s.length();
 
 
		// getting last index of string and storing into String Rev
		 for(int i=length-1;i>=0;i--)
			{
 
			   rev=rev+s.charAt(i);
			}
 
		System.out.println("Reverse of String: "+rev);
 
 
		 if(s.equalsIgnoreCase(rev))
		  {
 
		     System.out.println("It is Pelindrom");
		  }
 
 
		  else
		  {		
		      System.out.println("it is not pelindrom");
		  }
 
	}
 
}

output:

1
2
3
4
5
 
Enter a String
racecar
Reverse of String: racecar
It is Pelindrom

Leave a Reply

Your email address will not be published. Required fields are marked *