Thread: reversed a line of text using stack??

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    5

    reversed a line of text using stack??

    how to reversed a line of text using stack??

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    In a stack, everything in it should pop out in reverse.

    First In, Last Out

    http://www.cprogramming.com/tutorial...ory/stack.html
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stack>
    #define MAXSTACK  50
    
    struct StackRec
    {
       int top;
       char entry[MAXSTACK];
    };
    
    typedef struct StackRec Stack;
    
    
    void initializeStack(Stack *stackPtr)
    {
       stackPtr->top = -1;
    }
    
    int stackEmpty(const Stack *stackPtr)
    {
       if (stackPtr->top < 0) 
       {
          return 1;	//return true
       }
       else
       {
          return 0;	//return false
       }
    }
    
    int stackFull(const Stack *stackPtr)
    {
       if (stackPtr->top >= MAXSTACK - 1) 
       {
          return 1;	//return true
       }
       else 
       {
          return 0;	//return false
       }
    }
    
    void push(Stack *stackPtr, char item)
    {
       if (stackFull(stackPtr)) 
       {
          printf("Stack is full\n");
       }
       else 
       {
          stackPtr->top++;
          stackPtr->entry[stackPtr->top] = item;
       }
    }
    
    float pop(Stack *stackPtr)
    {
       float item;
    
       if (stackEmpty(stackPtr))
       {
          printf("Stack is empty\n");
       }
       else 
       {
          item = stackPtr->entry[stackPtr->top];
          stackPtr->top--;
       }
    
       return item;
    }
    
    void main()
    {
       Stack theStack;
       float next;
       char str [50];
       int i;
    
       initializeStack(&theStack);
       printf("Please enter a string: ");
       while (scanf("%c", str) != EOF) {
          push(&theStack, next);
       }
       while (!stackEmpty(&theStack)) {
          next = pop(&theStack);
    	  for (i=0;i<SIZE;i++){
    	printf("i=%d",i);
          printf("%c \n", str[i]);
       }
       printf("\n");
    }
    }



    I want to know how to create the main part...

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40
    I want to know how to create the main part...
    You already have a main() in your program, what is it that you want help with?
    Thanks,
    Sahil

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    Quote Originally Posted by sahil_m
    You already have a main() in your program, what is it that you want help with?
    can u help me see what's wrong i had make??

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40
    I dont have a C compiler with me right now, if you have any syntactical errors, tell me about them, or else wait for someone else to help you.
    Thanks,
    Sahil

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    Quote Originally Posted by sahil_m
    I dont have a C compiler with me right now, if you have any syntactical errors, tell me about them, or else wait for someone else to help you.
    don't have syntax error, but the output is not what i want......

  8. #8
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    for (i=0;i<SIZE;i++){
    what is SIZE??not defined

    Code:
    char str [50];
    while (scanf("%c", str) != EOF) {
          push(&theStack, next);
       }
    scanf returns the no. of values stored.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  9. #9
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    You are storing char,reading as float and printing as int. what are you upto.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  10. #10
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stack>
    #define MAXSTACK  50
    
    struct StackRec
    {
       int top;
       char entry[MAXSTACK];
    };
    
    typedef struct StackRec Stack;
    
    
    void initializeStack(Stack *stackPtr)
    {
       stackPtr->top = -1;
    }
    
    int stackEmpty(const Stack *stackPtr)
    {
       if (stackPtr->top < 0) 
       {
          return 1;	//return true
       }
       else
       {
          return 0;	//return false
       }
    }
    
    int stackFull(const Stack *stackPtr)
    {
       if (stackPtr->top >= MAXSTACK - 1) 
       {
          return 1;	//return true
       }
       else 
       {
          return 0;	//return false
       }
    }
    
    void push(Stack *stackPtr, char item)
    {
       if (stackFull(stackPtr)) 
       {
          printf("Stack is full\n");
       }
       else 
       {
          stackPtr->top++;
          stackPtr->entry[stackPtr->top] = item;
       }
    }
    
    char pop(Stack *stackPtr)
    {
       char item;
    
       if (stackEmpty(stackPtr))
       {
          printf("Stack is empty\n");
       }
       else 
       {
          item = stackPtr->entry[stackPtr->top];
          stackPtr->top--;
       }
    
       return item;
    }
    
    int main()
    {
       Stack theStack;
       char next;
       
       initializeStack(&theStack);
       printf("Please enter a string: ");
       while ((next=getchar()) != EOF && next!='\n')
        {
          push(&theStack,next);
        }
       while (!stackEmpty(&theStack)) {
          next = pop(&theStack);
    	  printf("%c ", next);
       }
       printf("\n");
    
    getchar();
    return 0;
    }
    Few rectifications in your program.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    stack1, stack2
    
    while not at end of input
         push( stack1, this letter )
    
    while not empty stack1
        push( stack2, pop( stack1 ) )
    Nice and simple.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Position FILE pointer to a specified line of Text file
    By jessica_xie in forum C Programming
    Replies: 2
    Last Post: 02-04-2005, 03:52 PM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. text line termination
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 09-09-2001, 04:39 AM