diff options
| author | Hyder <hyder@hyderhadi.xyz> | 2025-08-11 18:41:14 +0300 |
|---|---|---|
| committer | Hyder <hyder@hyderhadi.xyz> | 2025-08-11 18:41:14 +0300 |
| commit | fc946f55c53bd287e8ca5ebaa1a136b5ae4ac725 (patch) | |
| tree | 7a688d57bd4fcd6d6e4a648ed636d2afc0fa675e | |
| parent | 6c96292759f1f29cc5b499039351bc91f5eb2daa (diff) | |
I tried to make the VACC more elegant and readable function hopefully :D
| -rw-r--r-- | VACC/VowelsAndConsonantCounter.c | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/VACC/VowelsAndConsonantCounter.c b/VACC/VowelsAndConsonantCounter.c index 8cf0554..cff8641 100644 --- a/VACC/VowelsAndConsonantCounter.c +++ b/VACC/VowelsAndConsonantCounter.c @@ -1,31 +1,41 @@ # include <stdio.h> -void countConsonantandVowel(char input[1000]) { - char vowels[10] = "aeiouAEIOU"; - int count_vowels = 0; - int count_consonants = 0; - int spaces = 0; +#define INCREMENT 1 +#define TRUE 1 +#define FALSE 0 +#define LENGTH_OF_VOWELS 10 + +void countConsonantAndVowel(char input[]) { + char vowels[LENGTH_OF_VOWELS] = "aeiouAEIOU"; + int total_vowels = 0; + int total_consonants = 0; + int total_spaces = 0; for (int i = 0; input[i] != '\0'; ++i) { - int flag_vowel = 0; - for (int n = 0; n < 10; ++n) { - if (input[i] == vowels[n]) { - count_vowels += 1; - flag_vowel = 1; + int flag_vowel = FALSE; + for (int index = 0; index < LENGTH_OF_VOWELS; ++index) { + if (input[i] == vowels[index]) { + total_vowels += INCREMENT; + flag_vowel = TRUE; break; } } if (input[i] == ' ' || input[i] == '\n' || input[i] == '\t') { - spaces += 1; + total_spaces += INCREMENT; } else if (!flag_vowel) { - count_consonants += 1; + total_consonants += INCREMENT; } } - printf("THE VOWELS ARE: %d, THE CONSONANTS ARE: %d, THE WHITE SPACE COUNT: %d\n\n", count_vowels, count_consonants, spaces); + // The (total_spaces - 1) at the end is to get rid off of the last new line character that fgets() function adds when pressing enter, as it gets counted as well + printf("THE VOWELS ARE: %d, THE CONSONANTS ARE: %d, THE WHITE SPACE COUNT: %d\n\n", total_vowels, total_consonants, total_spaces - 1); } int main () { - char INPUT[1000] = "hyder hadi abd-alghani"; - countConsonantandVowel(INPUT); + char INPUT[1000]; + printf("Enter lines to see numbers of vowels, consonants and spaces: \n\n"); + + while(fgets(INPUT, sizeof(INPUT), stdin) != NULL) { + countConsonantAndVowel(INPUT); + } return 0; }
\ No newline at end of file |
