How to reverse full sentence word by word.. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to reverse full sentence word by word..

For example : if user input-----> my first program.-----> output ---> program first my.

11th Jul 2019, 11:12 AM
Faramoz Khan
Faramoz Khan - avatar
3 Answers
+ 4
I made this cpp program for you Reverse string word by word https://code.sololearn.com/cbVY06J0czy8/?ref=app #include <iostream> #include <string> using namespace std; int main() { string input; // input sentence string output; // output sentence getline(cin, input); // get sentence input input = input + " "; // add an extra space at end of string (work around to add symmetry, all words end by space now) string word; // temporary variable to store current word for(char& c : input){ if(c != ' '){ word.push_back(c); // append the charecter to word }else{ output = word + " " + output; // word completed so add it to start of output sentence word.clear(); // clear word for next iteration } } cout << output; return 0; }
11th Jul 2019, 11:59 AM
Om Kumar
Om Kumar - avatar
0
- read the input. - split them into array of string with space as delimiter - print the array one by one from the back
11th Jul 2019, 11:32 AM
Taste
Taste - avatar
0
Thanks bro....
11th Jul 2019, 1:41 PM
Faramoz Khan
Faramoz Khan - avatar