Thread: using Stacks & Queues to compare Strings

  1. #1
    Registered User
    Join Date
    Nov 2002

    Question using Stacks & Queues to compare Strings

    i'm trying to write a program where i use one stack and one queue, each of size 10, to find strings that are palindromes (spelled same forward and backward). i have my ItemType.h, Stack.h, Stack.cxx, Queue.h, and Queue.cxx all done. it is my driver that i'm working on now so i can put it all together but its confusing me. here's what i have so far:

    #include <iostream.h>
    #include <fstream.h>
    #include <iomanip.h>
    //#include <cctype.h>
    #include "stack.h"
    int main()
            ifstream infile;
            ofstream outfile;
            outfile << setprecision(2);
            ItemType item;
            StackType s;
              { s.Push(item);}
              catch (PushOnFullStack error)
               outfile << "Error!" << endl;
              { s.Pop(item);
                  outfile << item << endl;}
              catch (PopOnEmptyStack error)
              outfile << "Error!" <<endl;
    cout<<"done for chstack"<<endl;
    return 0;
    also, i'm pulling from my info i have to compare from a file (hence, "infile") and implemented exception handling as well. so far this thing runs...just that my oufile displays everything in it straight down a page...not even attempting to compare it out output it nicely yet.

    i'm just stumped on how to also implement the whole "queue" part in the driver so i can use it along with stack in comparing these strings of text i'm looking at.

    any help or hints at all would be appreciated, and if i'm being too broad in what i wrote or not clear enough, i'm sorry.
    eskimos have feelings too...

  2. #2
    Registered User
    Join Date
    Feb 2003
    Remember that items come off a queue in the same order they went on. Items come off a stack in reverse order.

    Have a stack and queue both of type char. Go through the string one char at a time placing each char onto the stack and onto the queue.

    Now remove the first char from the stack, remove the first char from the queue, & compare them. If equal, do the next char, and the next...

    If you empty the stack & queue & all the chars were the same, it was a palindrome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 08:01 PM
  2. how do i compare first letters in two strings?
    By MalickT in forum C Programming
    Replies: 8
    Last Post: 04-20-2008, 05:47 PM
  3. compare strings not working
    By gtriarhos in forum C Programming
    Replies: 7
    Last Post: 09-29-2005, 12:51 PM
  4. stacks and queues
    By j0hnb in forum C Programming
    Replies: 4
    Last Post: 04-16-2003, 09:44 PM
  5. how do i compare strings
    By bart in forum C++ Programming
    Replies: 17
    Last Post: 08-30-2001, 09:17 PM