Trying to solve the problem using C++ constructs. Reference to each word in the sentence is taken and reversed. But the changes are not seen in the original sentence.
class Solution {
public:
string reverseWords(string s) {
istringstream ss(s);
for(auto w = istream_iterator<string>(ss); w != istream_iterator<string>(); w++)
{
/* changes of the below 2 lines are not reflected in the main sentence*/
string &str = const_cast<string&>(*w);
reverse(str.begin(),str.end());
}
reverse(s.begin(),s.end());
return s;
}
};
Aucun commentaire:
Enregistrer un commentaire