fueanta

Reversed Sentence( by words ) 02

Sep 6th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. // Cpp program to get the reversed version of a sentence [reversed by words]
  2. // created on July, 2016
  3. // Author: fueanta
  4. // Algorithm Credit: skb50bd
  5.  
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. int main() {
  10.     string line,rev_line;
  11.     cout << "Given String: ";
  12.     getline(cin,line);
  13.     for(int i = line.length(); i >= 0 ; i--) {
  14.         if (line[i] == ' ') {
  15.             for (int j= i+1; line[j] != ' ' && line[j] != '\0' ;j++){
  16.                 rev_line+=line[j];
  17.             }
  18.             rev_line+=" ";
  19.         }
  20.         else if (i == 0) {
  21.             for (int j=0; line[j] != ' ' && line[j] != '\0' ;j++)
  22.                 rev_line+=line[j];
  23.         }
  24.     }
  25.     cout << "\nReversed String: " << rev_line << endl;
  26.     return 0;
  27. }
Add Comment
Please, Sign In to add comment