Program :
// Palindrome:
// A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization).
// Note: The word comes from the Greek "palin" (again) and "dromos" (way/direction), meaning "running back again."
import java.util.Scanner;
class Palindrome{
// Example 1:
/*
public static void main(String args[]){
System.out.print("Enter a Random Number:-");p
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int orginal= num;
// reversing number
int rev =0, rmd;
while(num > 0){
rmd = num %10;
rev = rev * 10 + rmd;
num = num / 10;
}
if(rev == orginal){
System.out.println(orginal+ " is a palindrome number!");
}
else{
System.out.println(orginal +" is a not palindrome number");
}
} */
// Example 2: Check Character is palindrome or not ?
static boolean palindrome(String str){
int left =0;
int right = str.length()-1;
while (left < right){
if(str.charAt(left) != str.charAt(right)){
return false;
}
left++;
right--;
}
return true;
}
public static void main(String args[]){
String textString = "MaMa"; // Not Palindrome
// String textString = "Ram"; // is this Plaindrome
if(palindrome(textString)){
System.out.println(textString +" is a palindrome");
}else{
System.out.println(textString +" is a not palindrome");
}
}
}
Answer:
MaMa is a not palindrome
0 Comments