This is a Java Program to Check whether a String is a Palindrome. A palindrome string is a string that is same after reverse.
package com.javaguides.collections.utils; import java.util.Scanner; public class CheckPalindrome { public static void main(String args[]) { String str, rev = ""; Scanner sc = new Scanner(System.in); System.out.println("Enter a string: "); str = sc.nextLine(); int length = str.length(); for ( int i = length - 1; i >= 0; i-- ) { rev = rev + str.charAt(i); } if (str.equals(rev)) { System.out.println(str+" is a palindrome"); } else { System.out.println(str+" is not a palindrome"); } sc.close(); } }
Output
Enter a string:
radar
radar is a palindrome
Comments
Post a Comment