The solution you've been looking for


#1

string Solution::solve(string A) {

if(A.length()==1)
{
    return A;
}
stack<string> st;
int i=0;
while(i<A.length())
{
    string str="";
    while(i<A.length() && A[i]!=' ')
    {
        str+=A[i];
        i++;
    }
    if(i<=A.length() && str!="")
    {
        st.push(str);
    }
    i++;
}
string res=st.top();
st.pop();
while(!st.empty())
{
    res+=" ";
    res+=st.top();
    st.pop();
}
return res;

}