【C语言程序设计第四版】练习12-4

字母转换并统计行数

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


int main(void){
    
    
    char ch;
    int enter_count;
    FILE *fp;
    enter_count = 0;
    
    if ((fp=fopen("f12-2.txt", "r")) == NULL) {
        printf("File open errpr!\n");
        exit(0);
    }
    
    while ((ch=fgetc(fp))!=EOF) {
        if (ch >= 'A' && ch <= 'Z') {
            ch = 'a' + (ch - 'A');
        }
        if (ch == '\n') {
            enter_count++;
        }
        putchar(ch);
    }
    
    if ((fclose(fp))) {
        printf("Can not close the file!\n");
        exit(0);
    }
    puts("");
    
    printf("File enter count number is %d.\n", enter_count);
    
    return 0;
}

 

posted @ 2021-09-27 16:11  就是想学习  阅读(62)  评论(0编辑  收藏  举报