Kodewick
Start learning →
Basic←→0 / 50

Count character types

#029Basicc >= '0' && c <= '9'7 min

Finish count(s): count how many letters (upper or lower case), spaces, digits and other characters are in the string, and show the four numbers in that order. For "abc XYZ 2024 #1!": 6 letters, 3 spaces, 5 digits, 2 others (# and !), so it shows 6 3 5 2. The program counts two strings.

Should show
6 3 5 2
2 6 3 4
main.c
#include <stdio.h>

void count(const char *s) {
    int letters = 0, spaces = 0, digits = 0, others = 0;
    for (int i = 0; s[i] != '\0'; i++) {
        char c = s[i];
        // your code here: find which type c is and add 1 to that count
    }
    printf("%d %d %d %d\n", letters, spaces, digits, others);
}

int main(void) {
    count("abc XYZ 2024 #1!");
    count("x = 3 * y + 42;");
    return 0;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.