Java Program to Check string palindrome
Write a Java Program to Check whether input string palindrome or not. Below I have shared a program that will check a string palindrome in java or not.
In this program I am reversing the string and then comparing the reversed string with original string. I have used Scanner class to read the string from keyboard.
Procedure : :
- A string is taken as an input to the program. The length of the string is calculated using the string method
length()and is stored in the variable n.
- An empty string variable reverse is declared and the last character of the given string is appended to the reverse variable using the string method
- In this process, the reverse of the given string is stored in the reverse variable. The reversed string is compared with the given string using the string method
- If the reversed string equals the given string then the given string can be considered as a palindrome otherwise it is not a palindrome.
radar, madam, mom, dad , etc.
Not Palindrome: :
Codezclub, java, road, car, etc.
Here you will get program for string palindrome in java.The Program below is successfully compiled and run on the Windows System to produce desired output.So,Let’s check out the program below.
SOURCE CODE : :
public class String_Palindrome
public static void main(String args)
Scanner sc = new Scanner(System.in);
System.out.print("Enter your String which u want to check : ");
str = sc.nextLine();
System.out.println("After Reversing, String is: " + reverse);
System.out.println("Input String is Palindrome");
System.out.println("Input String is not a Palindrome");
OUTPUT : :
Enter your String which u want to check : RADAR
After Reversing, String is: RADAR
Input String is Palindrome
EXPLANATION : :
Now we shall write a program and get a detailed explanation after this.
|1.||sc||Scanner object||Instantiation of Scanner class for I/O operations. The scanner class exists in the java.util package which needs to be imported before class declaration|
|2.||str||String||This will hold the value generated through keyboard|
|3.||reverse||String||It will hold the final reversed string|
|4.||len||int||The length of string generated through keyboard|
|5.||main()||function||This function is the entry point of the program.|
If you find any difficulty to understand the program then you can ask your queries in the comment section.