Count Letters, Digits, Spaces
Completed
Read a line and print letters=L digits=D spaces=S others=O.
Input
One line.
Output
One line.
Example
Input
Hello World 2026!
Output
letters=10 digits=4 spaces=2 others=1
Editor
C
Hints
Use the ctype.h classification functions.
Solution explanation
One loop over the string can collect several counts at once.
Show solution
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char s[200];
int letters = 0, digits = 0, spaces = 0, others = 0;
fgets(s, 200, stdin);
s[strcspn(s, "\n")] = '\0';
for (int i = 0; s[i] != '\0'; i++) {
if (isalpha(s[i])) letters++;
else if (isdigit(s[i])) digits++;
else if (s[i] == ' ') spaces++;
else others++;
}
printf("letters=%d digits=%d spaces=%d others=%d\n", letters, digits, spaces, others);
return 0;
}