Title, enter a string, invert the word according to the space,
Problem Description:
1, the word composition: No space characters form a word
2. The input string can contain leading and trailing spaces, but the inverted characters cannot be included.
3. There are multiple spaces between each word
Method: First extract each word, each reversal.
Code:
#include <string>
#include <vector>
String reverseString1 (string& str)
{
Std::string::size_type POS;
std::string result;
str + = ""; Extend strings for ease of operation, preventing no trailing spaces
int size = Str.size ();
string ret;
for (int i = 0; i < size; i++)
{
pos = Str.find (", i);
if (Pos < size && pos! = i)
{
std::string s = str.substr (i, pos-i);
Reverse (S.begin (), S.end ());
RET + = s + "";
i = pos;
}
}
Ret.erase (--ret.end ()); Remove the last extra space
return ret;
Test Result: Input:
std::string Test ("I have a little dog.");
Test = reverseString1 (test);
Output: Test = "I evah a elttil. God"
C + + string inversion