2

I've been working on a palindrome and it won't support an even number of words. I'm not the best at coding. It supports words like "racecar" or "tacocat", but it won't let me use a word/name like "Hannah". I'm new at this coding stuff so anything would really be appreciated.


import java.util.Scanner;
public class Palindrome
{
    public static void main(String args [])
    {
        System.out.printf("\f");
        Scanner input = new Scanner(System.in);
        System.out.println("enter a word");
        String word = input.nextLine();
        int size = word.length();
        int correct = 0;
        int incorrect = 0;
        for (int count = 1; count < size; count++)
        {
            int start = (word.charAt(count));//starting
            int end = (word.charAt(size-count));//ending
            if (start == end)
                correct++;
            else
                incorrect++;
        }
        if (correct == 0)
            System.out.printf("%s is a palindrome", word);
        else 
            System.out.printf("%s is not a palindrome", word);
    } 
}
S.Ngin
  • 21
  • 6
  • 1
    just convert `word` to lower case first. `h` and `H` are indeed different chars in an ASCII table. OR do it on the chat level, i.e. `Character.toLowerCase()` – UninformedUser Nov 28 '18 at 06:41

5 Answers5

1

Your code has many problems:

  1. You are comparing characters of wrong indices. For example, you compare the second character (whose index is 1) to the last character (whose index is size - 1). count should be initialized to 0, and end should be word.charAt(size-count-1).

  2. You report the String to be a palindrome when correct == 0, when it should be incorrect == 0 (BTW you don't need a counter, just a boolean).

  3. If you want the check to be case insensitive, you can convert the String to lower case prior to running your loop.

This should work:

public static void main(String args [])
{
    System.out.printf("\f");
    Scanner input = new Scanner(System.in);
    System.out.println("enter a word");
    String word = input.nextLine().toLowerCase();
    int size = word.length();
    boolean isPalindrome = true;
    for (int count = 0; count < size; count++)
    {
        int start = (word.charAt(count));//starting
        int end = (word.charAt(size-count-1));//ending
        if (start != end) {
            isPalindrome = false;
            break;
        }
    }
    if (isPalindrome)
        System.out.printf("%s is a palindrome", word);
    else 
        System.out.printf("%s is not a palindrome", word);
} 
Eran
  • 387,369
  • 54
  • 702
  • 768
1

There are several mistakes in your code

You should convert everything to lowercase if you are planning to ignore capital letter in the checking, since it is identified differently in ASCII

For starting, you should start from index 0 instead of 1, to start from the first letter

For ending, you should start from index size-count-1 instead of size-count, to start from the last letter

You should check for incorrect == 0 instead of correct == 0 to determine if it is a palindrome

public static void main(String args[]) {
    System.out.printf("\f");
    Scanner input = new Scanner(System.in);
    System.out.println("enter a word");
    String word = input.nextLine().toLowerCase();
    int size = word.length();
    int correct = 0;
    int incorrect = 0;
    for (int count = 0; count < size; count++)
    {
        int start = (word.charAt(count)); //starting
        int end = (word.charAt(size-count-1)); //ending
        if (start == end)
            correct++;
        else
            incorrect++;
        System.out.println(start + " " + end);
    }
    if (incorrect == 0)
        System.out.printf("%s is a palindrome", word);
    else 
        System.out.printf("%s is not a palindrome", word);
}

Bonus: You could check for just half of the word instead of looping through the whole word

Andreas
  • 2,455
  • 10
  • 21
  • 24
1

First of all you should know that array in java start at 0, not one. so set your count from 0 not one.
Then, word.charAt(count) is a char so better have char variable instead of int.

It's seem that the algorithm you use to decide whether a word is a palindrome or not is by matching first char with last char, second char with second last char, and so on.
If that the case, you will only need to loop halfway for (int count = 1; count < size / 2; count++).

The last one is, you only need one variable to hold the status of palindrome, if your matching process ever find a false then break the loop and just set the isPalindrome status into false.

public static void main (String args[])
{
    Scanner input = new Scanner (System.in);
    System.out.println ("enter a word");
    String word = input.nextLine ();
    int size = word.length ();
    boolean isPalindrome = true;
    int maxIndex = size - 1;
    for (int count = 0; count < size / 2; count++)
    {
        char start = word.charAt (count);
        char end = word.charAt (maxIndex - count);
        if (start != end)
        {
            isPalindrome = false;
            break;
        }
    }
    if (isPalindrome)
        System.out.printf ("%s is a palindrome", word);
    else
        System.out.printf ("%s is not a palindrome", word);
}  

And bear in mind that java's String is case sensitive, so "Tiger" is different than "tiger". Hence, Hannah will not be treated as palindrome. If you want it to be case insensitive, just lowercase all the char in the word like this word = word.toLowerCase() before doing the macthing process.

efmalik
  • 110
  • 1
  • 10
0

you may use Stringbuilder to do palindrome check as below

public class test {

public static void main(String args [])
{
    System.out.print("\f");
    Scanner input = new Scanner(System.in);
    System.out.println("enter a word");
    String word = input.nextLine();

    StringBuilder originalStr = new StringBuilder(word);        
    String revString = originalStr.reverse().toString();


    if(word.equalsIgnoreCase(revString)) 
         System.out.print( word +" is a palindrome");
   else 
       System.out.print( word +" is not a palindrome");
} 
}
iamrajshah
  • 963
  • 1
  • 11
  • 23
Arun
  • 1
  • 3
0

Check palindrome function is very simple:

public boolean isPalindrome(String str) {
    if(str == null)
        return false;

    str = str.toLowerCase();

    for(int i = 0, j = str.length() - 1; i < j; i++, j--)
        if(str.charAt(i) != str.charAt(j))
            return false;

    return true;
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35