简述:
输入一串英文字符串,统计出现的单词数目。
流程图:
原理简述:
定义一个字符串数组char a[100],用于接收输入的字符串,输入字符串本处运用的gets()函数(作用是从终端输入一串字符到定义的字符数组中,函数返回值是数组地址)。为方便表示,字符c用于代表字符数组中的每一项。count统计单词数,word表示是否出现新的单词。(0表示未出现,1表示出现,出现新单词的含义是:除第一个字符外,若上一个字符为空格,下一个字符不为空字符,认为出现新单词),只考虑字母。
函数编写:
#include<string.h>
#include<stdio.h>
int countWord(void) {
char strArry[100];
char c;
int i;//循环变量
int word, count;//word为1表示新单词出现,count为单词计数
word = 0;
count = 0;
printf("input please:\n");
gets(strArry);
for (i = 0; (c = strArry[i]) != '\0'; i++)
if (c == ' ')
word = 0;
else
if (word == 0) {
word = 1;
count++;
}
return count;
}
函数调用和运行结果:
#include<stdio.h>
int main() {
int countWord(void);
printf("word numbers=%d", countWord());
return 0;
}
完结(学习中,勿喷我!!!哈哈哈)