C 统计用户输入的总行数和字符长度

C 统计用户输入的总行数和字符长度

#include <stdio.h>
#include <Windows.h>

int main(void) {
    char line[2048];
    int count = 0;
    int length = 0;
    
    printf("请输入任意多行:\n");
    
    while (1) {
        // gets如果遇到文件结束符返回0,文件结束符是Ctrl+z。
        if (gets(line) == 0) { //C11新标准需要使用gets_s(line, sizeof(line))
            break;
        }
        count++;
        length += strlen(line);
    }
    
    printf("你总共输入了%d行\n", count);
    printf("你输入的字符总长度为:%d\n", length);

    system("pause");
    return 0;
}

 

posted @ 2019-08-10 13:39  你爱过大海我爱过你  阅读(219)  评论(0)    收藏  举报