Palindrome String Example Java Program


Definition

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. Allowances may be made for adjustments to capital letters, punctuation, and word dividers. Famous examples include "A man, a plan, a canal, Panama!"

Palindrome String Example Program

import java.util.*;
class PalindromeString{
	public static void main(String args[]){
		String str1,str2= "";
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the string");
		str1= in.nextLine();
		int length = str1.length();
		for ( int i = length - 1; i >= 0; i-- ){
			str2 = str2 + str1.charAt(i);
		}
		if (str1.equals(str2)){
			System.out.println("The given string is a palindrome.");
		}
		else{
			System.out.println("The given string is not a palindrome.");
		}
	}	
}

Sample Output

Output is:
Enter the string
madam
The given string is a palindrome.