Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. // Compiled with: g++ -Wall -std=c++14 -pthread
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. string myReverse(string word){//el
  8.     string reversedWord = "";
  9.    
  10.     for(int i=word.length()-1; i>=0; i--){
  11.          reversedWord+=word[i];  
  12.     }
  13.     return reversedWord;//le
  14. }
  15.  
  16. int main(){
  17.     string inputString = "mo as   el ";
  18.     bool spaceAdded = false;
  19.     if(inputString[inputString.length()-1] != ' '){  
  20.         inputString += " ";
  21.         spaceAdded = true;
  22.     }
  23.    
  24.     int prevSpace=0, nextSpace = 0;
  25.     string output = "";
  26.     if(inputString.find(" ") == -1){//The string have only one word
  27.      output += myReverse(inputString);  
  28.     }else{
  29.  
  30.         while (nextSpace != -1){
  31.              prevSpace = nextSpace;//0 2 5 6 7
  32.              nextSpace = inputString.find(" ", nextSpace)+1;//2 5 6 7 10
  33.             if(nextSpace - prevSpace > 1){//If
  34.                  string word = inputString.substr(prevSpace, nextSpace-prevSpace);
  35.                  output += myReverse(word);
  36.                  output += " ";//om sa le
  37.             }
  38.         }
  39.     }
  40.     if(spaceAdded){
  41.         output.erase(output.length()-1);
  42.     }
  43.     cout << output << endl;
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement