Reversing a string using stack

This is a discussion on Reversing a string using stack within the C Programming forums, part of the General Programming Boards category; After compiling the program, it doesn't show any errors or warnings. When the output screen loads, it asks for the ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    11

    Reversing a string using stack

    After compiling the program, it doesn't show any errors or warnings. When the output screen loads, it asks for the input but doesn't show the output and program automatically closes. Any idea why?

    Code:
    #include<stdio.h>
    #include<conio.h>
    void push(char);
    void pop();
    int top=0;
    char stack[10],info;
    void main()
    {
    int i;
    char str[10];
    clrscr();
    printf("Enter a string: ");
    gets(str);
    for(i=0;str[i]!='\0';i++) {
        info=str[i];
        push(info);
      }
    pop();
    getch();
    }
    void push(char info)
    {
    stack[top]=info;
    top++;
    }
    void pop()
    {
    while(stack[top]!=NULL) {
        printf("%c",stack[top]);
        top--;
       }
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    It does not make sense for 'pop' to pop all items off. Just as 'push' only adds one item, 'pop' should only remove one item.

    As for the bug, stack[top] is where the next item will be pushed to, but in 'pop' you want to start looking at the one below there.
    However, you should also note that there is no NULL in your stack, so the while loop is broken anyway. You need another way of checking if the stack has anything in it or not. Perhaps you could use 'top'.

    You should also change your main to:
    Code:
    int main(void)
    , and never use 'gets'. 'gets' is widely known to be fundamentally broken, and cannot be fixed. You need to use 'fgets' instead.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    Quote Originally Posted by iMalc View Post
    It does not make sense for 'pop' to pop all items off. Just as 'push' only adds one item, 'pop' should only remove one item.

    As for the bug, stack[top] is where the next item will be pushed to, but in 'pop' you want to start looking at the one below there.
    However, you should also note that there is no NULL in your stack, so the while loop is broken anyway. You need another way of checking if the stack has anything in it or not. Perhaps you could use 'top'.
    I will fix the gets and void main points ASAP.
    I have used the while loop in the pop function to ensure that ALL the elements of stack get popped :O...or am i going about it the wrong way?

    What if i edit the pop() function like this-
    Code:
    void pop()
    {
    top--;
    while(stack[top]!=NULL) {   
       printf("%c",stack[top]);  
       top--;   
       } 
    }
    This ought to fix the issues about pop function looking the correct place.

    Note-THANK YOU SOO MUCH ...Problem solved xD

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Mayank Rikh View Post
    Problem solved xD
    No, not at all. You forgot to consider the additional comment from iMalc:

    Quote Originally Posted by iMalc View Post
    However, you should also note that there is no NULL in your stack, so the while loop is broken anyway. You need another way of checking if the stack has anything in it or not. Perhaps you could use 'top'.
    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reversing a string
    By Magi in forum C Programming
    Replies: 40
    Last Post: 11-02-2012, 01:58 PM
  2. reversing a string, again..
    By Disident in forum C Programming
    Replies: 5
    Last Post: 06-15-2009, 08:01 PM
  3. Reversing string
    By Shal in forum C++ Programming
    Replies: 6
    Last Post: 08-19-2006, 06:50 PM
  4. Reversing a string
    By michelle1 in forum C++ Programming
    Replies: 12
    Last Post: 06-27-2003, 06:37 AM
  5. Reversing a String
    By ToasterPas in forum C++ Programming
    Replies: 10
    Last Post: 08-14-2001, 12:20 PM

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