A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction.
To check whether a word is a palindrome I get th
public class palindrome {
public static void main(String[] args) {
StringBuffer strBuf1 = new StringBuffer("malayalam");
StringBuffer strBuf2 = new StringBuffer("malayalam");
strBuf2.reverse();
System.out.println(strBuf2);
System.out.println((strBuf1.toString()).equals(strBuf2.toString()));
if ((strBuf1.toString()).equals(strBuf2.toString()))
System.out.println("palindrome");
else
System.out.println("not a palindrome");
}
}
Checking palindrome for first half of the string with the rest, this case assumes removal of any white spaces.
public int isPalindrome(String a) {
//Remove all spaces and non alpha characters
String ab = a.replaceAll("[^A-Za-z0-9]", "").toLowerCase();
//System.out.println(ab);
for (int i=0; i<ab.length()/2; i++) {
if(ab.charAt(i) != ab.charAt((ab.length()-1)-i)) {
return 0;
}
}
return 1;
}
import java.util.Scanner;
public class Palindrom {
public static void main(String []args)
{
Scanner in = new Scanner(System.in);
String str= in.nextLine();
int x= str.length();
if(x%2!=0)
{
for(int i=0;i<x/2;i++)
{
if(str.charAt(i)==str.charAt(x-1-i))
{
continue;
}
else
{
System.out.println("String is not a palindrom");
break;
}
}
}
else
{
for(int i=0;i<=x/2;i++)
{
if(str.charAt(i)==str.charAt(x-1-i))
{
continue;
}
else
{
System.out.println("String is not a palindrom");
break;
}
}
}
}
}
Considering not letters in the words
public static boolean palindromeWords(String s ){
int left=0;
int right=s.length()-1;
while(left<=right){
while(left<right && !Character.isLetter(s.charAt(left))){
left++;
}
while(right>0 && !Character.isLetter(s.charAt(right))){
right--;
}
if((s.charAt(left++))!=(s.charAt(right--))){
return false;
}
}
return true;
}
———
@Test
public void testPalindromeWords(){
assertTrue(StringExercise.palindromeWords("ece"));
assertTrue(StringExercise.palindromeWords("kavak"));
assertFalse(StringExercise.palindromeWords("kavakdf"));
assertTrue(StringExercise.palindromeWords("akka"));
assertTrue(StringExercise.palindromeWords("??e@@c_--e"));
}
Here my analysis of the @Greg answer: componentsprogramming.com/palindromes
Sidenote: But, for me it is important to do it in a Generic way. The requirements are that the sequence is bidirectionally iterable and the elements of the sequence are comparables using equality. I don't know how to do it in Java, but, here is a C++ version, I don't know a better way to do it for bidirectional sequences.
template <BidirectionalIterator I>
requires( EqualityComparable< ValueType<I> > )
bool palindrome( I first, I last )
{
I m = middle(first, last);
auto rfirst = boost::make_reverse_iterator(last);
return std::equal(first, m, rfirst);
}
Complexity: linear-time,
If I is RandomAccessIterator: floor(n/2) comparissons and floor(n/2)*2 iterations
If I is BidirectionalIterator: floor(n/2) comparissons and floor(n/2)*2 iterations plus (3/2)*n iterations to find the middle ( middle function )
storage: O(1)
No dymamic allocated memory
import java.util.Scanner;
class main
{
public static void main(String []args)
{
Scanner sc = new Scanner(System.in);
String str = sc.next();
String reverse = new StringBuffer(str).reverse().toString();
if(str.equals(reverse))
System.out.println("Pallindrome");
else
System.out.println("Not Pallindrome");
}
}