string reverseWords(string s) { int n = s.size(); std::string reverse_string = ""; for ( int i = n-1 ; i > -1; --i ){ if (s[i] == ' '){ continue; } int count = 0; while ( i > -1 && s[i] != ' '){ --i; count++; } if (reverse_string != ""){ reverse_string.append(" "); } reverse_string.append(s.substr(i+1,count)); } return reverse_string; }
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