pc012.c (1316B)
1 /* Write a C function that receives a string (character array) and uses 2 pointers to count and return the total number of vowels and consonants in that string. */ 3 /* Author: Amit Dutta, Date: 02-12-2025 */ 4 5 #include <stdio.h> 6 #include <ctype.h> 7 8 void charCounter(char[], int *, int *); 9 10 int main() 11 { 12 char str[101]; 13 int vowelCount, consonantCount; 14 printf("Enter the string (Max: 100 character): "); 15 if (fgets(str, sizeof(str), stdin) == NULL) 16 { 17 printf("Error reading input.\n"); 18 return 1; 19 } 20 charCounter(str, &vowelCount, &consonantCount); 21 printf("\nVowel Count: %d", vowelCount); 22 printf("\nConsonant Count: %d", consonantCount); 23 printf("\nTotal Character: %d", vowelCount + consonantCount); 24 return 0; 25 } 26 27 void charCounter(char str[], int *vowelCount, int *consonantCount) 28 { 29 int tempVowelCount = 0, tempConsonantCount = 0; 30 while (*str != '\0') 31 { 32 char ch = tolower(*str); 33 if (isalpha(ch)) 34 { 35 if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') 36 { 37 tempVowelCount++; 38 } 39 else 40 { 41 tempConsonantCount++; 42 } 43 } 44 str++; 45 } 46 47 *vowelCount = tempVowelCount; 48 *consonantCount = tempConsonantCount; 49 }