Hello I'm trying to right a program that reverses the order or words in strings.
The thing is I don't know how to do this. I only know how to reverse each letter using strrev() and the for loop method like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <cstring>
usingnamespace std;
int main ()
{
char str[20];
cout << "Enter a string: ";
cin.getline(str, 19);
int sizestr = strlen(str);
for(int i=0;i<sizestr+1;sizestr--)
{
cout << str[sizestr];
}
return 0;
}
But I'm required to reverse the word order something like
A first step might be to identify each individual word in the string. There are many ways to do this, though a stringstream might be the simplest. Then generate a new string by inserting each word (and a separating space if necessary) at the beginning of the new string. http://www.cplusplus.com/reference/string/string/insert/
Or store all the individual words in an array or vector and loop in reverse through the stored words to build the new string.