Question: in C + + Chapter 9 Use an STL stack to reverse a line of input characters that are read into a string. Your stack
in C Chapter
Use an STL stack to reverse a line of input characters that are read into a string. Your stack should contain the characters of the user's string. Use getline for input it needs to be part of your C tool inventory.
NOTE: Do NOT write your own stack class.
A note on getline: Suppose I am doing the following
This program reverses a string using the STL stack
Enter your string of less than characters followed by an ENTER
a string input
Enter another? continue. Anything else to stop
Enter your string of less than characters followed by an ENTER
a second string
This code will not work by simply using the following loop:
int go ;
cout "This program reverses a string using the STL stack" endl;
whilego
cout "Enter your string of less than characters followed by an ENTER" endl;
char s char malloc;
cin.getlines
;
cout s endl;
cout "Enter another? continue. Anything else to stop" endl;
cin go;
Try it and see what happens! Also note that I never got rid of the memory I allocated with malloc your code must get rid of it Also, malloc outside the loop for more efficient code.
You must use a getcharpart of the cstdio library after cin go;
The reason is that when you enter you also use the Enter key. This is still in the buffer when you hit getline again! So you will read in
Also note that when you get something off of the STL stack you must use top to look at it followed by pop to remove it
Example:
This program reverses a string using the STL stack
Enter your string of less than characters followed by an Enter
m&m cheeto mayo
oyam oteehc m&m
Enter another? continue. Anything else to stop
Enter your string of less than characters followed by an Enter
celery lettuce apple
elppa ecuttel yrelec
Enter another? continue. Anything else to stop
Press any key to continue
The reason is that when you enter you also use the Enter key. This is still in the buffer when you hit getline
again! So you will read in In
Also note that when you get something off of the STL stack you must use top to look at it followed by pop
remove it
Example:
This program reverses a string using the STL stack
Enter your string of less than characters followed by an Enter
m&m cheeto mayo
oyam oteehc m&m
Enter another? continue. Anything else to stop
Enter your string of less than characters followed by an Enter
celery lettuce apple
elppa ecuttel yrelec
Enter another? continue. Anything else to stop
Press any key to continue
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
