class Solution { public: //Function to reverse words in a given string. string reverseWords(string S) { int n=S.size(); stack<string>st; string si=""; for(int i=0;i<n;i++) { if(S[i]=='.') { st.push(si); st.push("."); si=""; } else { si+=S[i];} } st.push(si); string an=""; int t=st.size(); for(int i=0;i<t;i++) { an.append(st.top()); st.pop(); } return an; } };
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