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; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter