Palindrome (HELP)

Discussion in 'C++' started by Lorin Leone, Nov 12, 2003.

  1. Lorin Leone

    Lorin Leone Guest

    Can anyone help me modify the program so that it recognizes strings
    like "Anna" as palindromes. To make the program "case-insensitive."
    using the built-in C++ function "toupper". and so that it recognizes
    strings like
    "race car" as palindromes, have the program ignore spaces in the input
    string.

    Here is my code:


    #include <iostream>
    #include <iomanip>
    #include <string>

    using namespace std;

    #include "Stack.h"
    #include "queue.h"

    int main()
    {

    Stack the_stack;
    Queue the_queue;
    char cur_char;
    char tos;
    char foq;
    bool s;
    bool is_pal;

    cout << "input string: ";
    cin.get(cur_char);

    while (cur_char != '\n')
    {
    the_stack.push (cur_char));
    the_queue.enqueue (cur_char);
    cin.get (cur_char);
    }


    is_pal = true;

    while(not the_queue.is_empty() and is_pal)
    {
    the_stack.get_top(tos, s);
    the_queue.get_front(foq, s);

    the_stack.pop(s);
    the_queue.dequeue(s);
    if(tos != foq)

    is_pal = false;

    }

    cout << endl;

    if( is_pal )

    cout << "String is a palindrome." << endl;

    else

    cout << "String is NOT a palindrome." << endl;


    return 0;

    }
    Lorin Leone, Nov 12, 2003
    #1
    1. Advertising

  2. Lorin Leone

    Chris Theis Guest

    "Lorin Leone" <> wrote in message
    news:...
    > Can anyone help me modify the program so that it recognizes strings
    > like "Anna" as palindromes. To make the program "case-insensitive."
    > using the built-in C++ function "toupper". and so that it recognizes
    > strings like
    > "race car" as palindromes, have the program ignore spaces in the input
    > string.
    >
    > Here is my code:
    >
    >
    > #include <iostream>
    > #include <iomanip>
    > #include <string>
    >
    > using namespace std;
    >
    > #include "Stack.h"
    > #include "queue.h"
    >
    > int main()
    > {
    >
    > Stack the_stack;
    > Queue the_queue;
    > char cur_char;
    > char tos;
    > char foq;
    > bool s;
    > bool is_pal;
    >
    > cout << "input string: ";
    > cin.get(cur_char);
    >
    > while (cur_char != '\n')
    > {
    > the_stack.push (cur_char));
    > the_queue.enqueue (cur_char);
    > cin.get (cur_char);
    > }
    >
    >
    > is_pal = true;
    >
    > while(not the_queue.is_empty() and is_pal)
    > {
    > the_stack.get_top(tos, s);
    > the_queue.get_front(foq, s);
    >
    > the_stack.pop(s);
    > the_queue.dequeue(s);
    > if(tos != foq)
    >
    > is_pal = false;
    >
    > }
    >
    > cout << endl;
    >
    > if( is_pal )
    >
    > cout << "String is a palindrome." << endl;
    >
    > else
    >
    > cout << "String is NOT a palindrome." << endl;
    >
    >
    > return 0;
    >
    > }


    Okay, first I'd suggest the following code which is more efficient to check
    for a palindrome:

    string Test;
    cin >> Test;
    bool bFlag = true;

    string::iterator IterFw = Test.begin();
    string::reverse_iterator IterBw = Test.rbegin();
    while( *(IterFw++) == *(IterBw++) && IterFw != Test.end() ) {
    bFlag = false;
    }

    if( !bFlag )
    cout << "it's a palindrome" << endl;
    else
    cout << "it's NOT a palindrome" << endl;

    Some more hints to solve your problem - before you feed the string to the
    palindrom algorithm you have to do some preprocessing like stripping the
    blanks. A possible but not very efficient way would be to go through the
    string character by character and copy those that are not blanks into a new
    string. Another way would be to consider the remove_if() function with a
    predicate that should look like
    std::bind2nd(std::equal_to<char>(), ' ')

    In order to make lower case comparisons you'll have to modify the condition
    of the while loop given above, a little bit. At the moment it compares
    character by character taking upper and lower case into account. I guess
    this should get you started. Otherwise try and post when you've got some
    more trouble.

    HTH
    Chris
    Chris Theis, Nov 12, 2003
    #2
    1. Advertising

  3. Lorin Leone

    .oO LGV Oo. Guest

    "Lorin Leone" <> a écrit dans le message de
    news:...
    > Can anyone help me modify the program so that it recognizes strings
    > like "Anna" as palindromes. To make the program "case-insensitive."
    > using the built-in C++ function "toupper". and so that it recognizes
    > strings like
    > "race car" as palindromes, have the program ignore spaces in the input
    > string.
    >
    > Here is my code:


    'sounds like a homework assignment... if you wrote the code you provide, you
    shouldn't have major difficulties to modify it in order to obtain the
    desired behaviour...
    .oO LGV Oo., Nov 12, 2003
    #3
  4. Chris Theis wrote:
    > Okay, first I'd suggest the following code which is more efficient to check
    > for a palindrome:
    >
    > string Test;
    > cin >> Test;
    > bool bFlag = true;
    >
    > string::iterator IterFw = Test.begin();
    > string::reverse_iterator IterBw = Test.rbegin();
    > while( *(IterFw++) == *(IterBw++) && IterFw != Test.end() ) {
    > bFlag = false;
    > }
    >


    This code is more efficient than the original, but still could be done
    better. You actually do twice the work that is necessary by comparing
    until the end of the string. In fact, it would be enough to compare up
    to the middle character.

    In addition, setting the boolean flag inside the loop is obsolete. You
    can distinguish the two cases just by testing which of the terms in the
    and-expression led to leaving the loop.

    M.
    M. Baumgartner, Nov 12, 2003
    #4
  5. Lorin Leone

    Chris Theis Guest

    "M. Baumgartner" <> wrote in message
    news:...
    > Chris Theis wrote:
    > > Okay, first I'd suggest the following code which is more efficient to

    check
    > > for a palindrome:
    > >
    > > string Test;
    > > cin >> Test;
    > > bool bFlag = true;
    > >
    > > string::iterator IterFw = Test.begin();
    > > string::reverse_iterator IterBw = Test.rbegin();
    > > while( *(IterFw++) == *(IterBw++) && IterFw != Test.end() ) {
    > > bFlag = false;
    > > }
    > >

    >
    > This code is more efficient than the original, but still could be done
    > better. You actually do twice the work that is necessary by comparing
    > until the end of the string. In fact, it would be enough to compare up
    > to the middle character.
    >
    > In addition, setting the boolean flag inside the loop is obsolete. You
    > can distinguish the two cases just by testing which of the terms in the
    > and-expression led to leaving the loop.
    >
    > M.
    >


    You're of course absolutely right. I just think that the OP is not a very
    experienced programmer thus introducing to many nifty things might confuse
    more than it helps :) This looks very much like an assignment and IMHO some
    thinking and room for improvement should be left to the original author.

    Cheers
    Chris
    Chris Theis, Nov 13, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Runic911

    Palindrome

    Runic911, Nov 13, 2003, in forum: Python
    Replies:
    24
    Views:
    1,560
    Andrew Dalke
    Nov 15, 2003
  2. Tim Churches

    Re: Re: Palindrome

    Tim Churches, Nov 13, 2003, in forum: Python
    Replies:
    2
    Views:
    720
    yousafzai
    Jun 5, 2011
  3. Pierre Quentel

    Re: Palindrome

    Pierre Quentel, Nov 13, 2003, in forum: Python
    Replies:
    2
    Views:
    494
    Francis Avila
    Nov 13, 2003
  4. cat_dog_ass

    Palindrome using StringBuffer

    cat_dog_ass, Jan 23, 2007, in forum: Java
    Replies:
    4
    Views:
    2,867
    abhi2varma
    Jan 5, 2013
  5. Tung Chau
    Replies:
    1
    Views:
    457
    SM Ryan
    Aug 6, 2004
Loading...

Share This Page