For this question, you must first flip the entire string by letter, and then flip the string within the word, focusing on splitting the word, set the first and last signs to indicate the start and end positions of a word. The first or single space of a consecutive space is the end of the previous word, the next position of the last consecutive space is the start of the next word.
# Include <iostream>
# Define for if (1)
Using namespace std;
// Inverse Function of words, transmitted using char array
Void WordDesc (char str []);
Void main (){
Char str [] = "I am a student ";
Cout <"before processing:" <str <endl;
WordDesc (str );
Cout <"after processing:" <str <endl;
}
Void WordDesc (char str []) {
Int len = strlen (str); // String Length
Char temp; // temporary character for exchange
// Replace all characters first
For (int I = 0; I! = Len/2; I ++)
{
Temp = str [I];
Str [I] = str [len-i-1];
Str [len-i-1] = temp;
}
// Replace the value with the characters in each word.
Int first (0); // mark the start subscript of a word
Int last (0); // mark the end subscript of a word
For (int I = 0; I! = Len; I ++)
{
// Encounter a space
If (''= str [I])
{
// Multiple consecutive Spaces
If (''= str [I-1])
{
// Move the header pointer and perform the next loop
First ++;
Continue;
}
// A word before a space
// Mark the end as the previous one
Last = I-1;
// Process words
For (int j = first; j! = Last-(last-first)/2; j ++)
{
Temp = str [j];
Str [j] = str [last-j + first];
Str [last-j + first] = temp;
}
// Move the flag header to the next one
First = I + 1;
}
// Not a space, but to the end
Else if (I = len-1)
{
// Mark the end as current
Last = I;
// Process words
For (int j = first; j! = Last-(last-first)/2; j ++)
{
Temp = str [j];
Str [j] = str [last-j + first];
Str [last-j + first] = temp;
}
}
// Neither space nor tail
Else
{
NULL;
}
}
}
Running result:
Author: "cainiao changes"