程序可检测字符串,字母数,单词数,缺陷是遇到 isn’t 这种词会分辨成两个单词。
/*编写一个程序,在遇到EOF之前,把输入作为字符流读取。该程序要
报告平均每个单词的字母数。不要把空白统计为单词的字母。实际上,标点
符号也不应该统计,但是现在暂时不同考虑这么多(如果你比较在意这点,
考虑使用ctype.h系列中的ispunct()函数)。
*/
#include<stdio.h>
#include<stdio.h>
#include<stdbool.h>
int main(void)
{
int ch,i=0,j=0;
float k;
bool tf = false;
while ((ch = getchar()) != EOF)
{
if(isalpha(ch))
{
tf = true;
i++; //记录字母数;
}
else if (tf)
{
tf = false;
j++; //记录单词数;
}
else
continue;
}
printf("字母数为:%d\n单词数为:%d\n平均每个单词的字母数为:%4.2f", i, j,k= (float)i/j);
return 0;
}