Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Compiled with: g++ -Wall -std=c++14 -pthread
- #include <iostream>
- using namespace std;
- string myReverse(string word){//el
- string reversedWord = "";
- for(int i=word.length()-1; i>=0; i--){
- reversedWord+=word[i];
- }
- return reversedWord;//le
- }
- int main(){
- string inputString = "mo as el ";
- bool spaceAdded = false;
- if(inputString[inputString.length()-1] != ' '){
- inputString += " ";
- spaceAdded = true;
- }
- int prevSpace=0, nextSpace = 0;
- string output = "";
- if(inputString.find(" ") == -1){//The string have only one word
- output += myReverse(inputString);
- }else{
- while (nextSpace != -1){
- prevSpace = nextSpace;//0 2 5 6 7
- nextSpace = inputString.find(" ", nextSpace)+1;//2 5 6 7 10
- if(nextSpace - prevSpace > 1){//If
- string word = inputString.substr(prevSpace, nextSpace-prevSpace);
- output += myReverse(word);
- output += " ";//om sa le
- }
- }
- }
- if(spaceAdded){
- output.erase(output.length()-1);
- }
- cout << output << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement