I think I'm the first to get homework done lol

This commit is contained in:
juan 2021-10-12 20:56:06 +08:00
parent dbff279288
commit 1aa3e03e87
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7

33
pset3/6-chars.c Normal file
View file

@ -0,0 +1,33 @@
#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);
}