Step-by-step write algorithm (word count)

Source: Internet
Author: User

Text: Step by step write algorithm (Word statistics)


"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


In the interview session, there is a topic is also the examiner's favorite topic: If you count a paragraph consisting of characters and and spaces in a string of how many words?

In fact, the reason why asked this topic, the examiner's purpose is to understand how much you know about the state machine.

(1) Analysis of the problem

From the topic, if a string is processed, then there are several situations: initial state, character state, space state, end state. So how do you move between these states?

Initial state: If the input symbol is a space, then enter the space state, if it is a character, then enter the character state, and the number of words +1, if it is the end of the state, then directly return;

Character state: If the input symbol is a space, then enter the space state; if it is a character, then nothing is done; if it is finished, return directly;

Space status: If the input symbol is a space, then do nothing, if it is a character, then enter the character state, and the number of words +1, if the end of the state, then directly return.

/*          input is character *           -------->    character status----------* |                               |-->*      initial state  input character  |  |  Enter space            End status *          |                                 -->*          --------->    space status----------|*            input is a space */

(2) write the corresponding code according to 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 = INI T_state;char value; if (NULL = = pStr) return 0;while (value = *pstr++) {switch (state) {case Init_state:if ('! = value) Count + +, state = Word_state;elsestate = Space_state;break;case word_state:if ("= = value) state = Space_state;else if (' + ' = = *p STR) return count;break;case space_state:if (' '! = value ') Count + +, state = Word_state;else if (' + = = *pstr) return count;br Eak;default:break;}} return count;}

(3) Write test cases to verify that the code is written correctly

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 the programmer, it is the same as another method of callback function registration, so that we often use in the daily development of a method

2) State machine is the important content of computer network communication, want to deepen the understanding of TCP-IP protocol stack of friends especially need to focus on mastering


Step-by-step write algorithm (word count)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.