Thread: how to reversed a line of text using stack??

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

    how to reversed a line of text using stack??

    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<MAXSTACK;i++){
    	printf("i=%d",i);
          printf("%c \n", str[i]);
       }
       printf("Reversed line: ");
       printf("Is palindrome?");
    }
    }
    Help me see what's wrong i make??

  2. #2

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    printf("Please enter a string: ");
    while (scanf("%c", str) != EOF)
    push(&theStack, next);

    I don't use C I/O very much, but the above just looks wrong. It appears to me that scanf() is expecting the address of a char and you are giving it the address of a string. So I would either:
    a) obtain user input using scanf("%s", str) to read in the entire string outside the while loop and then use a loop to read each char from the string to the stack one at a time.
    b) change it somehow so I didn't read each char (scanf("%c", ) to the same address (str) each time through the while loop.

    Personally, I would favor option a.
    You're only born perfect.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    #include <stack>
    ?????

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