Skip to content

Latest commit

 

History

History
34 lines (28 loc) · 746 Bytes

3.md

File metadata and controls

34 lines (28 loc) · 746 Bytes
string reverseWords(string s) {
        
        int i=0,n=s.size(),j;
        string ans="";
        string temp="";
        while(i<s.size())
        {
            while(i<n && s[i]==' ')
            {
                i++;
            }

            j=i+1;
            while(j<n && s[j]!=' ')
            {
                j++;
            }
            temp =s.substr(i,j-i);
            
             if (!temp.empty()) {
                if (ans.empty()) 
                    ans = temp;
                else
                    ans = temp + " " + ans;
            }


            i=j+1;    
        }
        return ans;
    }