C语言 求大佬救一救新手小白!!

qq_45730227 2019-10-09 08:29:04
1.编写一个C程序,打开并逐字阅读文本文件,直到你遇到文件结束优先车道(EOF)。
2.把文本打印在屏幕上。
3. 计算文本中的单词长度,算出即长度为1的单词出现的次数,长度为2的单词出现的次数,一直到单词长度达到25个字母。 输出如下所示 length 2 : 3 occurrences 
length 3 : 1 occurrence 
length 4 : 4 occurrences 
length 5 : 2 occurrences 
length 6 : 1 occurrence 
length 7 : 1 occurrence  谢谢
...全文
57 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
wowpH 2019-10-10
  • 打赏
  • 举报
回复
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main() {
	FILE* file;
	file = fopen("test.txt", "r");
	if (file == NULL) {
		printf("the file : test.txt not found!\n");
		exit(-1);// stdlib.h
	}
	int length[26];
	memset(length, 0, sizeof(length));
	while (!feof(file)) {
		char str[26];
		memset(str, 0 , sizeof(str));
		int tmp = fscanf(file, "%s", str);
		++length[strlen(str)];// string.h
	}
	for (int i = 1; i < 26; ++i) {
		if (length[i] > 0) {
			printf("length %d : %d\n", i, length[i]);
		}
	}
	fclose(file);
	return 0;
}
/****************************************************
编译环境:Visual Studio 2019
测试样例:
test.txt文件
a ab afe
fsge eogej iohger
fwjie jwei
fhw
fe
e
e
wtkengwo
wtkengwo
wtkengwo
你好

输出:
length 1 : 3
length 2 : 2
length 3 : 2
length 4 : 3
length 5 : 2
length 6 : 1
length 8 : 3
请按任意键继续. . .
****************************************************/

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧