Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: s = “Let’s take LeetCode contest”
Output: “s’teL ekat edoCteeL tsetnoc”
Example 2:
Input: s = “God Ding”
Output: “doG gniD”
Solution:
string reverseWords(string s) {
int n = s.length();
if(n==0){
return s;
}
stringstream check1(s);
string interm;
string ans = "";
while(getline(check1,interm,' ')){
reverse(interm.begin(), interm.end());
ans += interm;
ans += " ";
}
return ans.substr(0, n);
}
};