习题6-1 分类统计字符个数 (15 分)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define MAXS 15

void StringCount(char s[]);
void ReadString(char s[]); /* 由裁判实现,略去不表 */

int main()
{
    char s[MAXS];

    ReadString(s);
    StringCount(s);

    system("pause");
    return 0;
}

/* Your function will be put here */
void StringCount(char s[]) {
    int letter = 0;
    int blank = 0;
    int digit = 0;
    int other = 0;
    for (int i = 0; i < 10; i++) {
        if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'))
            letter++;
        else if (s[i] == ' ' || s[i] == '\n')
            blank++;
        else if (s[i] >= '0' && s[i] <= '9')
            digit++;
        else
            other++;
    }
    printf("letter = %d, blank = %d, digit = %d, other = %d\n", letter, blank, digit, other);
}
void ReadString(char s[]) { 
    for (int i = 0; i < 10; i++)
        scanf("%c", &s[i]);
}

如若把10换成其他,会出错。

posted @ 2022-02-14 11:18  JamesGordan  阅读(60)  评论(0)    收藏  举报