0
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String word;
    String c;
    int x, count, count1;
    System.out.println("Please enter a word:");
    word=in.nextLine();
    x=word.length();
    count1=x;
    for(count=0;count<x;count++) {
        c=word.substring((count1)-1,count1);
        System.out.print(c);
        count1--;
    }`
}

Hi everyone, before I made this code that printed out a word backwards but now I am wondering how I can turn this into a palindrome checker but I am confused because the final product which is actually a word made out of a bunch of String c's put together, so I don't know how to compare it back to the original word that was entered. Help?

syntagma
  • 23,346
  • 16
  • 78
  • 134

2 Answers2

0

Start with a method signature and a value that you will return from it:

private boolean isPalindrome(String word1, String word2) {
  boolean isPalindrome = true; // let's initiate it to true
  // ...
  return isPalindrome;
} 

Then, you may use what you already have (I commented parts that are not needed:

x=word.length();
for(count=0;count<x;count++) {
    // c=word.substring((count1)-1,count1);
    // you can use word.charAt(count); instead
    // System.out.print(c);
    //count1--;
}`

Inside the loop, compare word1.charAt(count) with word2.charAt(count) and change isPalindrome to true if these don't match.

You may also want to check first if both strings have same length.

syntagma
  • 23,346
  • 16
  • 78
  • 134
0

Edit: Add another simple way to check for a palindrom:

public static boolean isPalindrom(String s) {
  return new StringBuilder(s).reverse().toString().equals(s);
}

I see 2 other possibilities to turn this in a palindrom-checker if you're not allowed to use StringBuilder(i changed your existing code a little bit)

  1. compare the chars of the first half of the string to the second half, something like this:

     boolean isPalindrom = true;
    
     for (int i= 0; i < (word.length()-1)/2; i++) {
         if(word.charAt(i) != word.charAt(word.length()-1-i))
         {
             isPalindrom = false;
         }
     }
    
     System.out.println(word + " is a palindrom: " + isPalindrom);
    

or

  1. use your reversed String, save it to another String-variable and then check it it is equal to your word. Something like this:

     public static void main(String[] args) {
         Scanner in = new Scanner(System.in);
         String word;
    
         System.out.println("Please enter a word:");
         word = in.nextLine();
    
         String reverseString = "";
    
         for (int i = 0; i < word.length(); i++) {
             reverseString = reverseString + word.charAt(word.length()-1-i);;
         }
    
         System.out.println("original: " + word + ", reversed: " + reverseString);
    
         boolean isPalindrom = word.equals(reverseString);
    
         System.out.println(word + " is a palindrom: " + isPalindrom);
     }
    

Maybe this is also helpful for you: Check string for palindrome

csalmhof
  • 1,820
  • 2
  • 15
  • 24