BUPT-homework/semester1/pset3/6-chars.c

34 lines
798 B
C
Raw Normal View History

#include <stdio.h>
// Function prototypes
void analyze(char *string);
int main(void) {
// Initialize a empty array
char str[100] = {};
// Get input from stdin
fgets(str, 100, stdin);
// Analyze string
analyze(str);
return 0;
}
void analyze(char *string) {
int chars = 0, spaces = 0, nums = 0, others = 0;
for (int i = 0; (string[i] != '\n' && string[i] != '\0'); i++) {
if ((string[i] >= 'a' && string[i] <= 'z') ||
(string[i] >= 'A' && string[i] <= 'Z')) { // Is a character
chars++;
} else if (string[i] >= '0' && string[i] <= '9') { // Is a number
nums++;
} else if (string[i] == ' ') { // Is a space
spaces++;
} else { // Other ascii characters
others++;
}
}
printf("%i %i %i %i\n", chars, spaces, nums, others);
}