193030

01. Visteon 5. Reverse Words (A)

Jun 25th, 2021 (edited)
760
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. string ReverseString(string inputString)
  8. {
  9.     string tempString = inputString;
  10.     int firstSpace = tempString.find(" ");
  11.     reverse(tempString.begin(), tempString.end());
  12.     int lastSpace = tempString.size()  - tempString.find(" ");
  13.  
  14.     string firstWord = inputString.substr(0, firstSpace);
  15.     string secondWord = inputString.substr(lastSpace, inputString.size() - lastSpace);
  16.     string middleWord = inputString.substr(firstSpace, lastSpace - firstSpace);
  17.  
  18.     string newString = secondWord + middleWord + firstWord;
  19.    
  20.     return newString;
  21. }
  22.  
  23. int main()
  24. {
  25.     string str = "This is String";
  26.     ReverseString(str);
  27. }
RAW Paste Data Copied