Link to home
Start Free TrialLog in
Avatar of nuclearcane
nuclearcane

asked on

Printing a Line of Text in Reverse Order using a STACK

Hi All...I need to be able to have the user enter a line of text and then enter it into my STACK function and then print it out in reverse order.  Since I'm using LIFO logic here, I just figured if I had main enter each character into the STACK that I would have it printed in reverse order.  The problem is I don't know what approach to take in order to enter the line of text from the user.  Here is what I have so far...Please feel free to jump in ASAP because I need this done right away.  I will be awarding more points than usual for this reason.  Thank you.

Here is what I have so far but obviously doesn't work.

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

/* Global Declarations */
typedef struct node
{
    char data;          
    struct node *next;  
} STACKNODE;

/* Prototype Declarations */
void push(char key, STACKNODE **stack);

int main()
{
    /* Local Definitions */
    STACKNODE *stack;
    STACKNODE key[100];
    STACKNODE *pWalker;
    int linecount = 0;
    int i = 0;
    stack = NULL;
   
    /* Statements */
    printf("Enter a line of text?\n");
    do
    {
        scanf("%c", &key);
        push(key[i].data, &stack);
        i++;
    } while (key[i].data != '\0');

    printf("\nList contains:\n");                
   
    pWalker = stack;
    while (pWalker)
    {
        if (++linecount > 25)
        {
            linecount = 1;
            printf("\n");
        }
       
    printf(" %c", pWalker->data);
    pWalker = pWalker->next;
    }            
             
    system("pause");
    return 0;
}

void push(char key, STACKNODE **stack)
{
    STACKNODE *newnode;
    newnode=(STACKNODE *)malloc(sizeof(STACKNODE));
    newnode->data = key;
    newnode->next = (*stack);
    (*stack) = newnode;
}


Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi nuclearcane,

This looks like homework.  Still, the function to do what you want is so simple that it's tough to tell you how to solve this without giving you the code.  Still, you've made a good try, so here goes....

You need a very simple function that does two things.

1)  Check so see if we're at the end-of-string.  If not, the function calls itself passing the address of the next character.
2)  Print the current character.

void ReversePrint (char *string)
{
  if (*string == NULL)
    return;
  ReversePrint (string+1);
  putch (*string);
}


Good Luck!
Kent
Avatar of ppk_cbe
ppk_cbe

NuclearCane, What you are doing should work logically. I do see a small problem in the program.

You are getting input from the user into 'key', but are passing key[i].data to the push function. You can actually just read a char, and pass that char to the push function. Your 'key' variable can be a char instead of STACKNODE. Then get input from user using scanf as you do now, and pass that key to the push function.

So, changes from your code -

STACKNODE key[100] can be changed to char key;
and push(key[i].data, &stack); can be changed to push(key, &stack);

That should work. If you are still experiencing issues, please let us know :)
Avatar of nuclearcane

ASKER

Hi ppk_cbe,

Thank you so much for your suggestion...what I need to know is how do I should I go about getting the loop to terminate?? I modified my code as you suggested...but it doesn't seem to be executing anything.  

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

/* Global Declarations */
typedef struct node
{
    char data;          
    struct node *next;  
} STACKNODE;

/* Prototype Declarations */
void push(char key, STACKNODE **stack);


int main()
{
    /* Local Definitions */
    STACKNODE *stack;
    char key;
    STACKNODE *pWalker;
    int linecount = 0;
    int i = 0;
    stack = NULL;
   
    /* Statements */
     printf("Enter a line of text?\n");
    do
    {
        scanf("%c", &key);
        push(key, &stack);
    } while (key != '\0');

Another question is could I actually just pass one line or string of text and have this code work for me or is it only feasible right now for individual characters? thanks again.


   


You can read a whole line at one go and then in a loop, push all characters onto the stack

fgets (buffer, MAX_BUF, stdin);
This will get a line from user into buffer ... next

initialize ptr to beginning of buffer

while ( not end of line )
{
    push (current char pointed by ptr)
    increment ptr
}
ASKER CERTIFIED SOLUTION
Avatar of ppk_cbe
ppk_cbe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
awesome, it works! thanks ppk_cbe!