Palindrome detection

So, I am working on a program that identifies palindromes (words that are the same forward and backward). To do this, I push the contents of the string to the queue and stack one character at a time. Then I have a for loop that compares the stack entries to the items in the queue to see if they are the same. If each character matches, than we have a palindrome. Any characters that do not match the results in the match flag will be in error and break the loop. The problem is my for loop won't start. The program skips completely. This is actually telling me that my "match" variable is not being used. Any help would be greatly appreciated. Thanks for reading my post.

** I am sorry for my uncertainty. Stack and Queue are classes of my own design. They take a String input, divide the String into characters (char type), create a node for each character and concatenate them. Stack last in first order, queue first in first. blank 1 and blank 2 are empty nodes that act as start markers in each.

public void palVerify(String s)
{
    boolean match=true;

    //creates stack
    Stack backward=new Stack(blank1);
    backward.push(s);

    //creates queue
    Queue forward=new Queue(blank2);
    forward.enqueue(s);


    // THIS LOOP WONT RUN

    for (int i=0; i < s.length(); i++)
    {
        if (backward.readTop()==forward.readFront())
        {

            backward.pop();
            forward.dequeue();
        }
        else
        {
            match=false;
            break;
        }

    }

    if (match=true)
        System.out.println("This word is a Palindrome");
    else
        System.out.println("This word is not a Palindrome");


}

      

+3


source to share


3 answers


You never evaluate match

in your code, you only assign to it. This is why you get an unused variable warning.

You probably want to evaluate match

in the if statement at the bottom - replace with if(match=true)

(which assigns true to the match variable) with if (match)

(which evaluates the variable and branches based on its value).



In your for loop - it probably works (you can add System.out.println("Here");

to the beginning of it to prove it. However, you are comparing strings using ==

- string comparison must use the equals

.

I'm not sure what blank1

and blank2

; I assume they are members or static variables declared outside of your method. I don't think your logic is for palindrome detection (comparison backward.readTop

and forward.readFront

does what you think of it, unless you are using your own versions Stack

and Queue

)

+1


source


public class JustForShow {

    public boolean isPalindrome(String text){
        if(text == null || text.isEmpty())
            return false;

        char []arr = text.toCharArray();
        for (int i = 0,y = (arr.length - 1); i<y; i++,y--) {
            if(arr[i] != arr[y]){
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        String palind = "ABCDDCBA";
        JustForShow jfs = new JustForShow();
        System.out.println(jfs.isPalindrome(palind));
    }
}

      



-1


source


Here's a working solution:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.print("Enter a three-digit integer: ");
    int number = input.nextInt();

    if (number / 100 == number % 10)
       System.out.println(number + " is a palindrome");
    else 
       System.out.println(number + " is not a palindrome");
 }

      

-1


source







All Articles