Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 616
  • Last Modified:

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


0
nuclearcane
Asked:
nuclearcane
1 Solution
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
nuclearcaneAuthor Commented:
awesome, it works! thanks ppk_cbe!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now