From 1aa3e03e8787e049d4f25cf804bba1d3e70f262f Mon Sep 17 00:00:00 2001 From: juan Date: Tue, 12 Oct 2021 20:56:06 +0800 Subject: [PATCH] I think I'm the first to get homework done lol --- pset3/6-chars.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 pset3/6-chars.c 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); +}