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...

  • I am reading a text file and trying to store the information into an array so...

    I am reading a text file and trying to store the information into an array so I can use this in different parts of my program. So a dynamic array. So far I have been able to retrieve the information from the text file and organize it by category (User name,User ID, User password) I am having troubles allocating an array and storing the information correctly. Since I have different data types (int, string) do I need to create a...

  • Program a clean_string function that combines both the convert_lower and remove_punct functionality in one function. In...

    Program a clean_string function that combines both the convert_lower and remove_punct functionality in one function. In other words, take a string as a parameter and return a clean version of the string that has no punctuation, no spaces and is all lower case. #include <iostream> #include <string> using namespace std; // TODO: Add your clean_string function here int main() { string line;    getline(cin, line); while(line != "quit") {    cout << clean_string(line) << ": " << line << endl;...

  • 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.

  • I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program...

    I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program reads two input files whose lines are //ordered by a key data field. This program should merge //these two files, writing an output file that contains //all lines from both files ordered by the same key field. // //********************************************************* #include <iostream> #include<string> #include<fstream> //prototype void mergeTwoFiles (ifstream&,ifstream&, ofstream&); using namespace std; int main() {string inFile1,inFile2,outFile; // input and output files ifstream in1; ifstream in2;...

  • 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...

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