Reverse words in a given string

PHOTO EMBED

Thu Sep 29 2022 09:10:33 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar

class Solution
{
    public:
    //Function to reverse words in a given string.
    string reverseWords(string S) 
    { 
        
        vector<string>v;
        string word="";
        for(auto x:S)
        {
            if(x=='.')
            {
                v.push_back(word);
                v.push_back(".");
                word="";
                continue;
            }
            word=word+x;
        }
        v.push_back(word);
        string s="";
        reverse(v.begin(),v.end());
        for(auto x:v)
        {
            s.append(x);
        }
        return s;
    } 
};
content_copyCOPY

https://practice.geeksforgeeks.org/problems/reverse-words-in-a-given-string5459/1