#include <stdio.h> #include <ctype.h>

int main() { FILE *fp; char filename[] = "test.txt"; char ch; int alpha_count = 0, num_count = 0, other_count = 0;

fp = fopen(filename, "r");

if (fp == NULL) {
    printf("无法打开文件 %s\n", filename);
    return 1;
}

while ((ch = fgetc(fp)) != EOF) {
    if (isalpha(ch)) {
        alpha_count++;
    } else if (isdigit(ch)) {
        num_count++;
    } else {
        other_count++;
    }
}

printf("字母个数: %d\n", alpha_count);
printf("数字个数: %d\n", num_count);
printf("其它字符个数: %d\n", other_count);

fclose(fp);

return 0;

}

编程分别统计一个文本文件中字母、数字及其它字符的个数。编程思路如下首先在源文件所在的文件夹中准备一个文本文件 testtxt其中可包含任意键盘输入的数字和字符然后编程实现对testtxt文件中的内容进行读取。C语言代码

原文地址: https://www.cveoy.top/t/topic/b4JV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录