Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<string.h>
#include "stacksADT.h"
int main(void)
{
bool done = false;
char* dataPtr;
int N;STACK* stack;
STACK* newStack;stack = createStack();
newStack = createStack();while (!done)
{
dataPtr = (char*)malloc(sizeof(char));
printf ("Enter a word or /uN for undoing N words or /x to exit:");
scanf("%s", dataPtr);if(strcmp(dataPtr,"/x")==0 || fullStack (stack))//user quits entering
done = true;else //user wants to enter more words or undo
{
//undo part
if(strncmp(dataPtr,"/u",2)==0)//check first two entered characters if undo
{
N= atoi(dataPtr+2);
while(N--)
{
popStack(stack);
}} //if
else
{
pushStack(stack, dataPtr); // push in to stack
}} //else
} // while
while (!emptyStack(stack))
{
dataPtr = (char*)popStack(stack);
pushStack(newStack, dataPtr);
} // whilewhile (!emptyStack(newStack))
{
dataPtr = (char*)popStack(newStack);
printf("%s\t", dataPtr);free (dataPtr);
} // whilereturn 0;
}This is how the output looks like:
Eg:
Enter word: I
Enter word: love
Enter word: icecream
Enter word: /u2
Enter word: /x
Output: I loveI'm not getting this part right.
Eg:
Enter word : I
Enter word: love
Enter word: /u3
can undo only 2 words.
Enter word: icecream
Enter word: /x
Output: icecreamI need to add a printf statement to above code.this printf statement should be printed incase I input less words and try to undo more words.
printf("Can undo only %d words\n", ??);
what do I put inplace of ?? and where should this statement go??

![]() |
How do i change the backg...
|
Batch file Help
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |