PALINDRCHECK
PALINDRCHECK
Program
string reverseWords(string s)
{
stringstream stream(s);
vector<string> vec;
string temp;
string result;
while (getline(stream, temp, ' '))
vec.push_back(temp);
for (int i = vec.size() - 1; i >= 0; --i)
{
if (vec[i] == "") continue;
result += vec[i] + ((i != 0)?" ":"");
}
return result;
}
Comments
- the stringstream class initialized with the string
- vector to hold the tokens
- tmp and result
- getline takes characters and appends it to the tmp until delim is encountered