Thursday, October 15, 2015

Reverse a String Word wise

Given an input string, reverse the string word by word.
Example:
Given s = "the sky is blue",
return "blue is sky the".


#include <iostream>
#include <sstream>
#include <string>
using namespace std;

void reverseWords(string &A) {
       string str="";
       stringstream ss;
      string str1;
      ss<<A;
    while(ss>>str1)
    {
       
       if(str=="")
           str=str1;
        elsea
           str=str1+" "+str;
       
    }
    
    cout<<str;
    A=str;
}

int main()
{
  string str=" hello  hi    ola ----  world ";
  reverseWords(str);
  return 0;

}

No comments:

Post a Comment

Contributors

Translate