#include <iostream>
#include <string.h>
#include <stack>
using namespace std;
class Solution {
public:
string ss;
void reverseWords(string &s) {
stack<char> s1, s2;
int l = s.length();
char *a = new char[l + 1];
int i, start;
for (i = 0; i < l; i++)
if (s[i] != ' ')
break;
if (i>=l)
{
s = "";
return;
}
start = i;
for (i = s.length() - 1; i >= 0; i--)
if (s[i] != ' ')
break;
s[i + 1] = '\0';
for (i=start;s[i]!='\0'; i++)
s1.push(s[i]);
i = 0;
while (!s1.empty())
{
if (s1.top() == '\0')
{
s1.pop();
}
else if (s1.top() == ' ')
{
s1.pop();
if (!s2.empty()){
while (!s2.empty())
{
a[i++]=s2.top();
s2.pop();
}
if (!s1.empty())
{
a[i++] = ' ';
}
}
}
else
{
s2.push(s1.top());
s1.pop();
}
}
while (!s2.empty())
{
a[i++] = s2.top();
s2.pop();
}
a[i] = '\0';
s = a;
}
};