diff --git a/pset3/6-chars.c b/pset3/6-chars.c new file mode 100644 index 0000000..6303305 --- /dev/null +++ b/pset3/6-chars.c @@ -0,0 +1,33 @@ +#include + +// 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); +}