- #include<stdio.h>
- int function(char s[])
- {
- int i = 0;
- int flag = 1;
- int count = 0;
- for(i=0;s[i]!='\0'; i++)
- {
- if (s[i] == ' ')
- {
- flag = 1;
- }
- else if (flag == 1)
- {
- flag = 0;
- count++;
-
- }
- }
- return count;
- }
- int main()
- {
- char s[30];
- printf("请开始输入字符串:");
- gets(s);
- printf("\n字符串中单词的个数是:%d\n", function(s));
- return 0;
- }