[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
During the interview, the examiner liked one question: how many words are there in a string composed of characters and spaces?
In fact, the purpose of the examiner is to know how much you know about the state machine.
(1) Question Analysis
From the perspective of the question, if a string is processed, there may be the following situations: initial status, character status, space status, and end status. So how should we migrate between these statuses?
Initial status: If the input symbol is a space, enter the space status. If it is a character, enter the character status with the number of words plus 1. If it is an end state, return directly;
Character status: If the entered symbol is a space, it enters the space status; if it is a character, nothing will be done; if it is an end, it will be returned directly;
Space status: If the entered symbol is space, nothing will be done. If it is a character, it enters the character status, and the number of words + 1; if it ends, it will be returned directly.
/* The input is a character.
* --------> Character status ----------
* | -->
* Initial State input character | enter space to end the State
* | -->
* ---------> Space status ---------- |
* The input is a space.
*/
/* The input is a character.
* --------> Character status ----------
* | -->
* Initial State input character | enter space to end the State
* | -->
* ---------> Space status ---------- |
* The input is a space.
*/
(2) write the corresponding code based on the State migration process described above
Typedef enum {
INIT_STATE = 1,
WORD_STATE,
SPACE_STATE,
};
Int count_word_number (const char * pStr)
{
Int count = 0;
Int state = INIT_STATE;
Char value;
If (NULL = pStr)
Return 0;
While (value = * pStr ++ ){
Switch (state)
{
Case INIT_STATE:
If (''! = Value)
Count ++, state = WORD_STATE;
Else
State = SPACE_STATE;
Break;
Case WORD_STATE:
If (''= value)
State = SPACE_STATE;
Else if ('\ 0' = * pStr)
Return count;
Break;
Case SPACE_STATE:
If (''! = Value)
Count ++, state = WORD_STATE;
Else if ('\ 0' = * pStr)
Return count;
Break;
Default:
Break;
}
}
Return count;
}
Typedef enum {
INIT_STATE = 1,
WORD_STATE,
SPACE_STATE,
};
Int count_word_number (const char * pStr)
{
Int count = 0;
Int state = INIT_STATE;
Char value;
If (NULL = pStr)
Return 0;
While (value = * pStr ++ ){
Switch (state)
{
Case INIT_STATE:
If (''! = Value)
Count ++, state = WORD_STATE;
Else
State = SPACE_STATE;
Break;
Case WORD_STATE:
If (''= value)
State = SPACE_STATE;
Else if ('\ 0' = * pStr)
Return count;
Break;
Case SPACE_STATE:
If (''! = Value)
Count ++, state = WORD_STATE;
Else if ('\ 0' = * pStr)
Return count;
Break;
Default:
Break;
}
}
Return count;
}
(3) Write test cases to verify that the code is correctly written.
Void test ()
{
Assert (0 = count_word_number (NULL ));
Assert (0 = count_word_number (""));
Assert (1 = count_word_number ("hello "));
Assert (3 = count_word_number ("china baby hello "));
}
Void test ()
{
Assert (0 = count_word_number (NULL ));
Assert (0 = count_word_number (""));
Assert (1 = count_word_number ("hello "));
Assert (3 = count_word_number ("china baby hello "));
}
Summary:
1) the state machine is the basic skill of programmers. It is the same as Callback Function registration in another method, which is a frequently used method in daily development.
2) the state machine is an important part of computer network communication. If you want to learn more about the tcp-IP protocol stack