【C语言程序设计第四版】第十二章 程序设计题 1

第一题:统计文本文件中各类字符个数:分别统计一个文本文件中字母、数字及其其它字符的个数。

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

int main(void){
    int alphabet, digit, other;
    char ch;
    
    alphabet = digit = other = 0;
    
    
    FILE *fp;
    if ((fp = fopen("text.txt", "r")) == NULL) {
        printf("Open file error.
");
        exit(0);
    }
    
    while ((ch = fgetc(fp))!=EOF) {
        if (isalpha(ch)) {
            alphabet++;
        }else if(isdigit(ch)){
            digit++;
        }else{
            other++;
        }
    }if (fclose(fp)) {
        printf("Can not close the file!
");
        exit(0);
    }
  printf("This file alphabet is %d, digit is %d, other is %d.
", alphabet, digit, other);
  return 0;
}
原文地址:https://www.cnblogs.com/sidianok/p/15351763.html