Hi,I am a c++ beginner.
I am using vs,and I got a problem in my code which check a sentence whether a palindrome or not.

#include <iostream>
#include <stack>
#include <queue>
using namespace std;

int main ()
    bool palindrome = true;
    char character;
    StackType stack;
    Queue queue;
    char stackChar;
    char queChar;

    cout << "Enter a string; press return." << endl;

    while ( character != '\n' && character != '.')
        if ( isalpha (character))
                character = tolower(character);

        stack.Push ( character );
        queue. enqueue ( character );
        cin.get ( character );

    while ( palindrome && !queue.isEmpty () )
        stackChar = stack.Top ();
        stack.Pop ();
        queue.dequeue ( queChar );
        if ( stackChar != queChar )
            palindrome = false;

    if ( palindrome )
        cout << "String is a palindrome." << endl;
        cout << "String is not a palindrome." << endl;
    return 0;

Enter a string;press return.
String is a palindrome.

Enter a string;press return.
String is a palindrome.

It seems ok, but when I test this:

Enter a string;press return.
Able was I ere, I saw Elba.
String is not a palindrome.

but "Able was I ere, I saw Elba." in the assignment example is a palindrome.
can anyone tell me whats wrong with my code? please.

Recommended Answers

All 2 Replies

The problem is probably the comma

Do you have to count the punctuation also? Because in your example, you have a comma (',') and a dot ('.'), which, even though the words may be palindrome regarding the sentence, would point that your sentence is not a palindrome.

Also, is it a homework requirement to actually use implementations of queues and stacks in checking for palindroms (rather, do you need to implement functionalities of stack and queue, and as an example, you implemented to make use of the palindrome verification), or you just want to see if a sentence is a palindrome, and you thought it would be easier this way?

Be a part of the DaniWeb community

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