Write a Java application that asks the user to enter a word and then determines if this word is a palindrome. A palindrome is a word that is the reads the same when spelt backwards as it does forwards. For example, the words noon and madam are palindromes. Note, you need to ignore the case of the individual letters in the word should the user input the word in upper and lowercase. So, for example, madam, MadAm, and mAdaM are considered the same word and palindromes.

Recommended Answers

All 11 Replies

here is the coding which i have stareted!!

public class Palindrome{
public static void main(String[] args)
{
  //Do main here
}
public static boolean isPalindrome(String word) {
  int left = 0;             // index of leftmost unchecked char
  int right = word.length() -1; // index of the rightmost

  while (left < right) {       // continue until they reach center
     if (word.charAt(left) != word.charAt(right)) {
        return false;       // if chars are different, finished
     }
     left++;               // move left index toward the center
     right--;             // move right index toward the center
  }

  return true;             // if finished, all chars were same
} 
}

So, which part are you having trouble with?

Have you tested your code?

yeah finally got it ... i have checked it (but not rigorously ) and its working .

public class NewClass {
 public static void main (String [] args)
 {
     String str1 = new String("madame");
     int l,l1;
     boolean b = true ;
     char c1,c2;
     l1 = l = str1.length();          
     //l1 = str1.length();
     for ( int i=0;i<l1 ; i++)
     {
         c1 = str1.charAt(i);
         c2 = str1.charAt(l-1);
             if(c1 != c2)
             {      System.out.println("not a palindrome");
                 b = false ; 
                  break;  
             }
         l--;
     }
 if(b == true)
       System.out.println(" palindrome");
     
      
 
 }
}

i guess this should do ...

You could try this without using the "l","l1" int variables

public class NewClass {
 public static void main (String [] args)
 {
     String str1 = new String("madame");
     boolean b = true ;
     char c1,c2;
     int length = str1.length();
     for ( int i=0;i<(length/2); i++)
     {
         c1 = str1.charAt(i);
         c2 = str1.charAt(length-1 -i);
             if(c1 != c2)
             {      System.out.println("not a palindrome");
                 b = false ; 
                  break;  
             }
     }
 if(b == true)
       System.out.println(" palindrome");
 }
}

Also I think that your original code was correct. Have you tested it?

yes i have ran it and it works fine .
thanks for improving my code .well i knew it could also have been done in a shorter way than i did , its just that i wanted to be very comfortable with wat i was coding with .

yes i have ran it and it works fine .
thanks for improving my code .well i knew it could also have been done in a shorter way than i did , its just that i wanted to be very comfortable with wat i was coding with .

Well, you are correct. first write something that works and then see if you know how to improve it

well i follow the approach of analysis and then design and finally robustness of the code . that makes things easier . and skipping these basic fundamentals will create a lot of confusion n frustration .

Here's how I would do it:

String firstString = "wordtocheck";
String secondString = new StringBuffer(firstString).reverse().toString();
if(firstString.compareTo(secondString)==0){
System.out.println("This is a palindrome");
}else{
System.out.println("This is not a palindrome");
}

Yes this is cool,

but I would prefer StringBuilder instead StringBuffer. So try this,

String firstString = "wordtocheck";
String secondString = new StringBuilder(firstString).reverse().toString();
if(firstString.compareTo(secondString)==0){
System.out.println("This is a palindrome");
}else{
System.out.println("This is not a palindrome");
}

String builder because it is not synchronized and is very slightly faster?
Probably in this example there wont be a problem but in a multi thread application there would be an advantage.
Good point!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.