Exercise 3-4 of the title collection of "C Language Programming (3rd Edition)" of Zhejiang University Edition

Exercise 3-4 Statistical Characters (15 points)

This question requires writing a program, enter 10 characters, and count the number of English letters, spaces or carriage returns, numeric characters and other characters.
Input format:

Input 10 characters. The last carriage return indicates the end of the input, which is not counted.
Output format: output in the format of
letter = number of English letters, blank = number of spaces or carriage returns, digit = number of digit characters, other = number
of other characters in a line.
Input sample:
aZ &
09 Az
Output sample:
letter = 4, blank = 3, digit = 2, other = 1
Author
Yan Hui
Unit
Zhejiang University City College
Code length limit

16 KB
time limit
400 ms
memory limit
64 MB

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

int main(void) {
    
    
    char ch;
    int letter = 0, blank = 0, digit = 0, other = 0;

    for (int i = 0; i < 10; i++) {
    
    
        ch = getchar();
        if (isalpha(ch))
            letter++;
        else if (isalnum(ch))
            digit++;
        else if (ch == '\n' || ch == ' ')
            blank++;
        else
            other++;
    }
    printf("letter = %d, blank = %d, digit = %d, other = %d",
           letter, blank, digit, other);
    return 0;
}

Guess you like

Origin blog.csdn.net/DoMoreSpeakLess/article/details/109348256