用C语言写一个程序,统计123.txt中英文字符,数字字符,空白字符以及其他字符的个数

如题所述

第1个回答  2014-06-21
#include<stdio.h>
#include <stdlib.h>

int main()
{
    // 数字个数
    int digistNum = 0;
    // 英文字母
    int englishNum = 0;

    // 空格个数
    int spaceNum = 0;

    int otherNum = 0;


    FILE* file;

    if (NULL == (file = fopen("D:\\123.txt", "r")))
    {
        printf("文件打开失败\n");
        return -1;
    }

    char str[1024] = {0};
    while (fgets(str, sizeof(str), file) != NULL)
    {
        for (int i = 0; i < strlen(str);i++)
        {
            if (str[i] <= '9' && str[i] >= '0')
            {
                digistNum++;
            }
            else if ((str[i] >= 'A' && str[i] <= 'Z')
                || (str[i]) >= 'a' && str[i] <= 'z')
            {
                englishNum++;
            }
            else if (' ' ==str[i])
            {
                spaceNum++;
            }
            else
            {
                otherNum++;
            }
        }

    }

    fclose(file);

    printf("英文字母: %d, 数字: %d, 空格: %d, 其他字符: %d\n",
        englishNum, digistNum, spaceNum, otherNum);
    return 0;
}

运行结果如下:

相似回答
大家正在搜