69,369
社区成员
发帖
与我相关
我的任务
分享
#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
请按任意键继续. . .
****************************************************/