Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Cpp program to get the reversed version of a sentence [reversed by words]
- // created on July, 2016
- // Author: fueanta
- // Algorithm Credit: skb50bd
- #include <iostream>
- using namespace std;
- int main() {
- string line,rev_line;
- cout << "Given String: ";
- getline(cin,line);
- for(int i = line.length(); i >= 0 ; i--) {
- if (line[i] == ' ') {
- for (int j= i+1; line[j] != ' ' && line[j] != '\0' ;j++){
- rev_line+=line[j];
- }
- rev_line+=" ";
- }
- else if (i == 0) {
- for (int j=0; line[j] != ' ' && line[j] != '\0' ;j++)
- rev_line+=line[j];
- }
- }
- cout << "\nReversed String: " << rev_line << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment