I think I'm the first to get homework done lol
This commit is contained in:
parent
dbff279288
commit
1aa3e03e87
33
pset3/6-chars.c
Normal file
33
pset3/6-chars.c
Normal 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);
|
||||
}
|
Loading…
Reference in a new issue