Thread: Character stack ADT program

  1. #1
    Registered User alice's Avatar
    Join Date
    Mar 2004

    Character stack ADT program

    how can I write a program, using ADT that process a text strings,

    void push(char ch); // pushes the character ch onto the stack;
    char pop(); // pops and returns the top character;
    int isEmpty(); // returns 1 if the stack is empty, 0 otherwise;
    int reverseSentence(char str[]); // main function;
    ie, if
    is "ABC D2EF. GHI3J. KLM", then after calling the function
    becomes "FED CBA. JIHG. MLK". The function should also return 3, indicating that there are 3 sentences.

    Can someone spent a little time to help on my question? thk a lot.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    A stack ADT is dreadfully simple if you don't worry about error handling.
    static char   stack[BUFSIZ];
    static size_t top = 0;
    static void
    push(char ch)
      stack[top++] = ch;
    static char
      return stack[--top];
    static int
      return top == 0;
    As for the reverseSentence function, really all you have to do is walk down the sentence looking for punctuation. Before you find a sentence terminator, push each character on to the stack. Once you find the terminator, start popping and saving to the beginning of the sentence. You would probably need two indices. The first would be your index for pushing and searching, the second would be your index for saving the beginning of a sentence and rewriting the reversed sentence. Something like this:
    pusher := string
    while not end do
      saver := pusher
      while not end and string[pusher] != '.' do
        pusher := pusher + 1
      while not isEmpty() do
        string[saver] := pop()
        saver := saver + 1
      pusher := pusher + 1; # Remove '.'
      while not end and isspace(string[pusher]) do
        pusher := pusher + 1
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Infix, Postfix, Pseudo-Calculator using Stack ADT
    By sangken in forum C Programming
    Replies: 9
    Last Post: 09-08-2006, 08:17 AM
  4. Understanding ADT Stack
    By smitsky in forum C++ Programming
    Replies: 8
    Last Post: 11-09-2004, 10:23 PM
  5. Character counting program
    By TankCDR in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2002, 10:01 PM