Question

In: Computer Science

c++ problem: Use an STL stack to reverse a line of input characters that are read...

c++ problem:

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.

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 80 characters followed by an ENTER

a string input

Enter another? 1 = continue. Anything else to stop

1

Enter your string of less than 80 characters followed by an ENTER

a second string

This code will not work by simply using the following loop:

       int go = 1;

       cout << "This program reverses a string using the STL stack" << endl;

       while(go == 1){

              cout << "Enter your string of less than 80 characters followed by an ENTER" << endl;

            char* s = (char *)malloc(80);

              cin.getline(s,81,'\n');

              cout << s << endl;

              cout << "Enter another? 1 = 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 getchar() (part of the cstdio library) after cin >> go;

The reason is that when you enter 1 you also use the Enter key. This is still in the buffer when you hit getline again! So you will read in '\n'

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 80 characters followed by an Enter

m&m cheeto mayo

oyam oteehc m&m

Enter another? 1 = continue. Anything else to stop

1

Enter your string of less than 80 characters followed by an Enter

celery lettuce apple

elppa ecuttel yrelec

Enter another? 1 = continue. Anything else to stop

0

Press any key to continue . . .

Solutions

Expert Solution

#include<iostream>
#include<stack>
#include<cstdlib>
#include<string>
using namespace std;

int main(){
   stack <char> stack;
   int go = 1;
   cout<<"This program reverses a string using STL stack"<<endl;
   while(go==1){
       cout<<"Enter the string of less than 80 characters followed by an ENTER"<<endl;
       char* s = (char *)malloc(80);
       cin.getline(s,81,'\n');
       int i=0;
       while(s[i]!='\0')
           stack.push(s[i++]);
      
       while(!stack.empty()){
           cout<<stack.top();
           stack.pop();
       }
       cout<<endl;
       cout<<"Enter another? 1 = continue. Anything else to stop"<<endl;
       cin>>go;
   }
   return 0;
}


Related Solutions

Design and implement a C++ program read in a whole line of characters as the input...
Design and implement a C++ program read in a whole line of characters as the input string; count and display how many times how frequently (among the letters) each (case insensitive) letter appears in the above mentioned input string; Sample program execution: An example of executing such a program is shown below. Note that the user input is in italic font. Please enter a line of characters: This is a really long line of characters! There are 41 characters in...
Do not use c-string, arrays, and read input characters one by one. Also use : s.push_back(ch);....
Do not use c-string, arrays, and read input characters one by one. Also use : s.push_back(ch);. Code a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered .The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Eg: Enter a sequence of characters: abAa1121X+&$%$dc[space] Distinct characters are: abA12X+&$%dc. Use C++
Using STL stack class, implement in C++ a function that checks for balanced braces { },...
Using STL stack class, implement in C++ a function that checks for balanced braces { }, in a given string / arithmetic expressions.
Using STL stack class, implement in C++ the following pseudocodefunctioncheckBraces(aString: string) that checks for...
Using STL stack class, implement in C++ the following pseudocode functioncheckBraces(aString: string) that checks for balanced braces { } in a given string /arithmetic expressions. Write a main() program to test the function.// Checks the string aString to verify that braces match.// Returns true if aString contains matching braces, false otherwise.checkBraces(aString: string): boolean{aStack = a new empty stackbalancedSoFar = truei =0// Tracks character position in stringwhile (balancedSoFar and i < length of aString) {ch = character at position i in...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix expression,
Write a java program to reverse element of a stack. For any given word (from input),...
Write a java program to reverse element of a stack. For any given word (from input), insert every character (from the word) into a stack. The output from the stack should be the same as the input. Your program should be using a stack and a queue to complete this process. 1. Push into stack 2. Pop from stack 3. Enqueue into queue 4. Dequeue from queue 5. Push into stack 6. Pop from stack and display java
java Programming Problem 1: Reverse Characters Create an application ReverseCharacters in which you use an array-based...
java Programming Problem 1: Reverse Characters Create an application ReverseCharacters in which you use an array-based stack to read a sentence from the keyboard, and reverse the order of the letters in each word. Print the initial sentence, as well as the result of the reversal operation. Your Tasks: Using the information given in your textbook, create a class named ArrayBasedStack, that will help you solve the problem given above by providing the stack operations you need for this application....
In c++, using stack structure, write a program that will take a sequence of characters (string)...
In c++, using stack structure, write a program that will take a sequence of characters (string) and determine whether it is a palindrome. Use the linked version of the stack.
PROBLEM STATEMENT: Using the list container from the STL, write a program that will read the...
PROBLEM STATEMENT: Using the list container from the STL, write a program that will read the information from a file into a list and then display the list to the screen. Add your name. Sort the list by id. Print the list again CODE: Use the provided disneyin2.txt file. Do not hard code the file name; get file name from user. Use the struct below struct student { char firstnm[20], lastnm[20]; int id, grade; }; You are to create a...
Write a C++ program to read characters from the keyboard until a '#' character is read....
Write a C++ program to read characters from the keyboard until a '#' character is read. Then the program will find and print the number of uppercase letters read from the keyboard.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT