Below is a simple and an efficient way for checking whether an input string is Palindrome or not
import java.util.Scanner;
public class Palindrome {
public static void main(String [] args){
System.out.println("Enter the string for palindrome check : ");
Scanner sc = new Scanner(System.in);
String original = sc.next();
boolean isPalindrome = isPalindrome(original);
if(isPalindrome){
System.out.println("The input string " + original + " is a palindrome");
}else{
System.out.println("The input string " + original + " is not a palindrome");
}
}
public static boolean isPalindrome(String original){
int length = original.length();
int i = 0;
while(length > 0){
if(original.charAt(i) != original.charAt(length -1)){
return false;
}
i++;
length--;
}
return true;
}
}
Hope you like it !!
No comments:
Post a Comment