Question

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
quit

then the output is:

ereht olleH
yeH


There is a mistake in my code I can not find. my output is : 

ereht olleH ereht olleHyeH

How can I fix this?

I saw some people use void or the function reverse but we didnt learn about that yet.

thank you for your help :)


#include

#include

using namespace std;


int main() {

   

  string input;

  getline (cin, input);

  string new_input;

 

 // until user input Quit quit q

 

 while ( input != "Quit" && input != "quit" && input != "q") {

 

 // output in reverse

 

 for (int i = input.length()-1; i >= 0; --i){

    new_input.push_back(input[i]);

  

  }

    cout << new_input << endl;

   cin >> input;  

    }


  return 0;

}


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

#include
#include
#include
#include

using namespace std;

string reverse(string orig_string);
int main() {
/*
* Declaring the constant
*/
   const int SIZE=100;
   /*
* Declaring the variables
*/
   int count=0;
   string arr[SIZE];
   string str;
  
   /*
* Getting the input entered by the user and
   * populate those strins into an array until the user enters "Quit" or "quit"
*/
   getline(cin,str);
   while(str.compare("quit")!=0 && str.compare("Quit")!=0 && str.compare("q")!=0 && str.compare("Q")!=0)
   {
       arr[count]=str;
       count++;
       getline(cin,str);
   }
  
   /*
* Displaying the strings reversely
*/
   cout<<"Displaying the reverse strings :"<   for(int i=0;i   {
       string rev=reverse(arr[i]);
       cout<   }
  
   return 0;
}
/*
* This function will reverse the string and return to the caller
*/
string reverse(string orig_string)
{
   string rev_string="";
  
/*
   * This for loop will reverse the string
   */
for(int i=orig_string.length()-1;i>=0;i--)
{
rev_string+=orig_string.at(i);
}
return rev_string;
}

============================================

Output:


answered by: ANURANJAN SARSAM
Add a comment
Know the answer?
Add Answer to:
Write a program that takes in a line of text as input, and outputs that line of text in reverse.
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 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 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...

  • C++ 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 quitthen the output is:ereht olleH yeH

  • Help with C++ reverse program with leading zeros. I need to put the line from the...

    Help with C++ reverse program with leading zeros. I need to put the line from the function that display the zeros in main not in the function. So how can I move the display with leading zeros in main. Thanks. Here is my code. #include <iostream> #include <cstdlib> #include <iostream> using std::cout; using std::cin; using std::endl; //function templates int reverseNum(int); int main() { //variables char buf[100]; int num; while (true) { //prompt user for input cout << "Enter the number...

  • ​c++ program that takes user input from the console and store the data into a linked list.

    c++ program that takes user input from the console and store the data into a linked list. The input made of 4 objects associated to a car: model, make, mileage and year. My program should take the how many inputs the user want to make, followed by the inputs themselves. After the input, my program should print the data inside the linked list. So far, the issue is that my program print some of the input, but not all. Input sample:3HondaSentra89002017ToyotaCamri1098271999NissanSentra87261987current...

  • Hello, The C++ question is: ------------------------------------------------------------------------------- Write a program that takes as input 2 strings from...

    Hello, The C++ question is: ------------------------------------------------------------------------------- Write a program that takes as input 2 strings from the user. Then it determines if one string is a permutation of the other string. If the program answers "yes" to the previous question, meaning the two strings are permutations of each other, determine if each string has all unique characters. --------------------------------------------------------------------------------- I have completed the first part but I am unsure on how to also determine if each string is all unique characters...

  • 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.Tell the user the program will read strings until "QUIT" is entered.LoopAsk user for a stringIf the string is "QUIT" break the loop, otherwise store data3. Inform user data will be displayed in reverse.4. Display ALL strings in reverse.5. Free the space used to store the strings.

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • Update the program in the bottom using C++ to fit the requirements specified in the assignment....

    Update the program in the bottom using C++ to fit the requirements specified in the assignment. Description For this assignment, you will be writing a single program that enters a loop in which each iteration prompts the user for two, single-line inputs. If the text of either one of the inputs is “quit”, the program should immediately exit. If “quit” is not found, each of these lines of input will be treated as a command line to be executed. These...

  • Hey, so i am trying to have my program read a text file using a structure...

    Hey, so i am trying to have my program read a text file using a structure but i also want to be able to modify the results(I kinda have this part but it could be better). I cant seem to get it to read the file(all the values come up as 0 and i'm not sure why because in my other program where it wrote to the txt file the values are on the txt file) i copied and pasted...

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