Character stack ADT program

This is a discussion on Character stack ADT program within the C Programming forums, part of the General Programming Boards category; how can I write a program, using ADT that process a text strings, Code: void push(char ch); // pushes the ...

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

    Character stack ADT program

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

    Code:
    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
    Code:
    str
    is "ABC D2EF. GHI3J. KLM", then after calling the function
    Code:
    str
    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
    Posts
    9,796
    A stack ADT is dreadfully simple if you don't worry about error handling.
    Code:
    static char   stack[BUFSIZ];
    static size_t top = 0;
    
    static void
    push(char ch)
    {
      stack[top++] = ch;
    }
    
    static char
    pop(void)
    {
      return stack[--top];
    }
    
    static int
    isEmpty(void)
    {
      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:
    Code:
    pusher := string
    
    while not end do
      saver := pusher
      while not end and string[pusher] != '.' do
        push(string[pusher])
        pusher := pusher + 1
      loop
      while not isEmpty() do
        string[saver] := pop()
        saver := saver + 1
      loop
      pusher := pusher + 1; # Remove '.'
      while not end and isspace(string[pusher]) do
        pusher := pusher + 1
    loop
    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, 02: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, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21