Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"
#include<iostream>
#include<string>
#include <algorithm>
#include<vector>
using namespace std;
class Solution {
public:
string reverseWords(string s) {
string s1="";
string s2="";
for (int i = 0; i < s.size(); i++)
{
s2 += s[i];
if (s[i] == ' ')
{
for (int j = s2.size() - 2; j >= 0; j--)
{
s1 += s2[j];
}
s1 += ' ';
s2 = "";
}
if (i == s.size() - 1)
{
for (int j = s2.size() - 1; j >= 0; j--)
{
s1 += s2[j];
}
}
}
return s1;
}
};
int main()
{
string s = "Let's take LeetCode contest";
for (int i = 0; i < s.size(); i++)
cout << s[i];
cout << endl;
Solution t;
s=t.reverseWords(s);
for (int i = 0; i < s.size(); i++)
cout << s[i];
cout << endl;
system("pause");
return 0;
}