统计一行字符的单词个数
统计一行字符的单词个数,单词间暂时约定用空格来分析
//统计一行字符的单词个数 #include<stdio.h> #include<string.h> #include<stdlib.h> int Is_Separate(char ch) { char strSeparate[] = {',', ' ', '?', '.'}; for (int i = 0; i < strlen(strSeparate); i++) { if (ch == strSeparate[i]) { return 1; } } return 0; } int main() { char strCharacter[] = " how are you? I am fine. what is your name? my name is xiao! "; int nSeparate = 1; int nWorld = 0; int nCount = 0; for (int i = 0; i < strlen(strCharacter); i++) { if (nSeparate && !Is_Separate(strCharacter[i])) { nSeparate = 0; nWorld = 1; nCount++; } if (nWorld && Is_Separate(strCharacter[i])) { nWorld = 0; nSeparate = 1; } } printf("%d\r\n", nCount); system("pause"); return 0; }