c编写程序文件内容读出,统计并显示其中的大写字母个数,小写字母个数,数字个数。
时间: 2024-05-07 20:15:11 浏览: 16
```c
#include <stdio.h>
#include <ctype.h>
int main() {
FILE *file_ptr = fopen("file.txt", "r"); // 打开文件
if (file_ptr == NULL) { // 检查文件是否打开成功
printf("File not found.\n");
return 1;
}
int uppercase_count = 0, lowercase_count = 0, digit_count = 0; // 统计变量初始化为0
char c;
while ((c = fgetc(file_ptr)) != EOF) { // 读取文件内容,直到文件结束
if (isupper(c)) { // 判断是否为大写字母
uppercase_count++;
} else if (islower(c)) { // 判断是否为小写字母
lowercase_count++;
} else if (isdigit(c)) { // 判断是否为数字
digit_count++;
}
}
printf("Uppercase letters count: %d\n", uppercase_count); // 输出统计结果
printf("Lowercase letters count: %d\n", lowercase_count);
printf("Digits count: %d\n", digit_count);
fclose(file_ptr); // 关闭文件
return 0;
}
```
注意:上述代码需要一个名为`file.txt`的文件来进行测试,可在代码所在目录下创建该文件并输入任意内容。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)