?
Solved

Printing a Line of Text in Reverse Order using a STACK

Posted on 2005-04-02
6
Medium Priority
?
613 Views
Last Modified: 2010-04-15
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
Comment
Question by:nuclearcane
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13690304
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
 
LVL 1

Expert Comment

by:ppk_cbe
ID: 13690430
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
 

Author Comment

by:nuclearcane
ID: 13690493
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
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!

 
LVL 45

Expert Comment

by:sunnycoder
ID: 13690510
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
 
LVL 1

Accepted Solution

by:
ppk_cbe earned 2000 total points
ID: 13690528
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
 

Author Comment

by:nuclearcane
ID: 13690541
awesome, it works! thanks ppk_cbe!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question