(用C程序编写)编写一个程序输入一行字符,统计其中有多少个单词。单词之间用一个或多个空格分隔开。

要求:调用一个单词库,单词要在词库里找得到
2024-12-07 07:46:02
推荐回答(1个)
回答(1):

也就是从文件中读取数据,你看下边的行吗?#includeint jishu(FILE *fp);int main()
{char str[16]; /*存放地址*/
int num;
FILE *fp;
printf("Input the address of the file:");
gets(str);
fp=fopen(str,"r");
if(!fp) exit(0);
num=jishu(fp);
printf("\nThe number of words is %d",num);
system("pause");
return 0;
}int jishu(FILE *fp)
{ char ch;
int number,target;
number=target=0;
while((ch=fgetc(fp))!=EOF)
{ if(ch!=' '&&target==0)
number++,target++; /*在空格后出现非空格,单词数量加一*/
else if(ch!=' ') target++; /*一个单词,计数器不变*/
else if (ch==' ') target=0; /**遇到空格,设置标志/
}
return number;
}
在dev c++编译器中运行成功……