#include <stdio.
h>
#include <ctype.h>
void count Vowels And Consonants(char *str, int *vowels, int
*consonants)
{
*vowels = *consonants = 0; char *ptr = str; while (*ptr !=
'\0')
{
char ch = tolower(*ptr); if (ch >= 'a' && ch <= 'z')
{
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
{
(*vowels)++;
} else
{
(*consonants)++;
}
}
ptr++;
}
}
int main()
{
char str[100]; int vowels,
consonants; printf("Enter a
string: "); fgets(str,
sizeof(str), stdin);
count Vowels And Consonants(str, &vowels, &consonants);
printf("Vowels: %d\n", vowels); printf("Consonants: %d\n",
consonants);
return 0;
}
Output:
Enter a string: JSS Acadamy of Technical Education
Vowels: 12
Consonants: 18
Process returned 0 (0x0) execution time : 25.571 s Press
any key to continue.
Algorithm Steps:
1. Start.
2. Initialize *vowels = 0 and *consonants = 0.
3. Initialize a pointer ptr pointing to str.
4. While the character pointed by ptr is not the null character ('\0'):
5. Convert the character to lowercase using tolower.
6. If the character is an alphabetic letter:
7. If it is a vowel ('a', 'e', 'i', 'o', 'u'), increment the *vowels counter.
8. Otherwise, increment the *consonants counter.
9. Move to the next character by incrementing ptr.
10. Print the values of vowels and consonants.
11. End.
Tracing:
1. Start of main() function: vowels and consonants are declared but not yet
initialized.
The string str[100] is declared.
2. User input: Program prompts: "Enter a string: "User enters: "Hello World!"
fgets() reads the input and stores it in str. The content of str is:str = "HellWorld!\n"
3. Call to count Vowels And Consonants():Function call: count Vowels And
Consonants (str, &vowels, &consonants);
vowels = 0; consonants = 0;
The string "Hello World!\n" is passed, along with the addresses of vowels and
consonants.
4. Inside count Vowels And Consonants():
Initialize a pointer ptr to point to the beginning of the string: ptr = str
Start the while loop: while (*ptr != '\0')