Question

C++ Program Takes Strings Until 'QUIT' Input then Displays Strings in Reverse Order

Write a C++ program that reads strings until "QUIT" input, which then displays all strings in the reverse order they were entered.


  1. Tell the user the program will read strings until "QUIT" is entered.

  2. Loop

    1. Ask user for a string

    2. If the string is "QUIT" break the loop, otherwise store data

3. Inform user data will be displayed in reverse.

4. Display ALL strings in reverse.

5. Free the space used to store the strings.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <cstring>

using namespace std;

// Drive code

int main()

{

  // variables declaration

  string str;

  string store[100];

  int i = 0;

  cout<<"Input\n";

  // infinite while loop that will break by user

  while (1){

    // getting string input from user

    getline(cin, str);

    // if user enters "QUIT" then

    if(str == "QUIT"){

      // break the loop

      break;

    }

    // otherwise

    else{

      // store the string in the array

      store[i] = str;

      // i incremented by 1

      i++;

    }

    

  }

  cout <<"\nOutput:\n";

  // output in reverse order

  for(int j=i- 1; j>=0; j--){

    cout << store[j] << endl;

  }

  return 0;

}



answered by: Allen
Add a comment
Answer #2
#include <iostream>

#include <cstring>

using namespace std;

int main()
{

  string str;

  string store[100];

  int i = 0;

  cout<<"Input\n";

  while (1){

    getline(cin, str);

    if(str == "QUIT"){

      break;

    }

    else{

      store[i] = str;

      i++;

    }

    

  }

  cout <<"\nString in reverse order:\n";
  for(int j=i- 1; j>=0; j--){

    cout << store[j] << endl;

  }
  return 0;

}


answered by: Shivani Sharma
Add a comment
Know the answer?
Add Answer to:
C++ Program Takes Strings Until 'QUIT' Input then Displays Strings in Reverse Order
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program that will create an array of Strings with 25 elements. Input Strings...

    Write a Java program that will create an array of Strings with 25 elements. Input Strings from the user and store them in the array until either the user enters “quit” or the array is full. HINT: the sentinel value is “quit” all lowercase. “Quit” or “QUIT” should not stop the loop. HINT: you have to use the equals method (not ==) when you are comparing two Strings. After the input is complete, print the Strings that the user entered...

  • JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a...

    JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a complete method that reads a series of Strings from the user. The user enters "end" to stop inputting words. Then, output the Strings in reverse order of how they were entered. Do not output the String “end”. Use a stack to accomplish this task. Invoke only the methods push, pop, peek, and isEmpty on the stack object. Here is an example of how the...

  • How to store a sentence of strings into a vector container with push_back until I enter...

    How to store a sentence of strings into a vector container with push_back until I enter and type "quit"? string str: vector < int > v: if (str != "quit") {//use push_back to store str to vector} BUILD 3 LOOPS TO DISPLAY THE USER DEFINED STRING SENTENCE WITH THE CONDITIONS: to print out the strings using a for loop with begin and end & differencing iterator pointer. In another loop using the C++11 auto keyword to simplify the declaration of...

  • Write a program that takes in a line of text as input, and outputs that line of text in reverse.

    Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text.Ex: If the input is:Hello there Hey quitthen the output is:ereht olleH yeHThere is a mistake in my code I can not find. my output is : ereht olleH ereht olleHyeHHow can I fix this?I saw some people use void or the function reverse but we didnt...

  • Write a program that takes in a line of text as input, and outputs that line of text in reverse.

     4.24 LAB: Print string in reverse Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or 'q" for the line of text. Ex: If the input is: Hello there Hey quit the output is: ereht ollel yeH

  • Write a Java program that reads a series of strings from a user until STOP is...

    Write a Java program that reads a series of strings from a user until STOP is entered. Each input consists of information about one student at the ABC Professional School. The input consists of the student’s last name (the first 15 characters with extra blanks on the right if the name is not 15 characters long), the student’s number (the next 4 characters, all digits, a number between 1000 and 5999), and the student’s program of study (one character, either...

  • Write a program that takes in a line of text as input, and outputs that line...

    Write a program that takes in a line of text as input, and outputs that line of text in reverse. You may assume that each line of text will not exceed 50 characters. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH уен Hint: Use the fgets function to read a string with spaces from the user...

  • (PYTHON) Write a program that displays the following menu:. , Write Algorithm for Code Geometry Calculator...

    (PYTHON) Write a program that displays the following menu:. , Write Algorithm for Code Geometry Calculator 1. Calculate the Area of a Circle 2. Calculate the Area of a Rectangle 3. Calculate the Area of a Triangle 4. Quit Enter your choice (1 - 4): If the user enters 1, the program should ask for the radius of the circle and then display its area. If the user enters 2, the program should ask for the length and width of...

  • // Group Names: // Date: // Program Description: //   Import required packages //--> //   Declare class...

    // Group Names: // Date: // Program Description: //   Import required packages //--> //   Declare class (SwitchDoLab) //--> {    //   Declare the main method    //-->    {        //   Declare Constant integers SUM = 1, FACTORIAL = 2, QUIT = 3.        //-->        //-->        //-->        //   Create an integer variable named choice to store user's option.        //-->        //   Create a Scanner object        //   Create...

  • Python code Write a program that will ask the user to input two strings, assign the...

    Python code Write a program that will ask the user to input two strings, assign the strings to variables m and n If the first or second string is less than 10 characters, the program must output a warning message that the string is too short The program must join the strings m and n with the join function The output must be displayed. Please name the program and provide at least one code comment (10)

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT