• C

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;
}


nuclearcaneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:
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
0
ppk_cbeCommented:
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 :)
0
nuclearcaneAuthor Commented:
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.


   


0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

sunnycoderCommented:
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
}
0
ppk_cbeCommented:
You can check for '\n' instead of '\0' as the loop terminator. Also, it is better to change your do while loop construct to a while construct. Also, I would replace scanf with a getchar()

while ((ch = getchar()) != '\n')
{
   push(...)
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nuclearcaneAuthor Commented:
awesome, it works! thanks ppk_cbe!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.