0
How to reverse full sentence word by word..
For example : if user input-----> my first program.-----> output ---> program first my.
3 Réponses
+ 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;
}
0
- read the input.
- split them into array of string with space as delimiter
- print the array one by one from the back
0
Thanks bro....