knakul853

Untitled

Jul 20th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     string reverseWords(string s) {
  4.        
  5.         istringstream in(s);
  6.        
  7.         vector<string>a;
  8.         string ans;
  9.         string word;
  10.         while(in >> word)
  11.         {
  12.             a.push_back(word);
  13.             //cout<<word<<" ";
  14.         }
  15.        
  16.         for(int i=(int)a.size()-1; i>=0; i--)
  17.         {
  18.             if(i == (int)a.size()-1)
  19.                 ans=a[i];
  20.             else
  21.              ans +=" "+a[i];
  22.             cout<<a[i]<<" ";
  23.         }
  24.        
  25.         return ans;
  26.     }
  27. };
Add Comment
Please, Sign In to add comment