C Program to count number of vowels and consonants in a string
Write a C Program to count the number of vowels and consonants in a string using loops or an array. How to find the count of vowels and consonants in a string. Logic to find the number of vowels and consonants in a string.
C Program to count number of vowels and consonants in a string using loop and conditional statement
#include <stdio.h>
void main()
{
char str[50];
int vowels = 0, consonants = 0, i;
printf("Enter string: ");
gets(str);
for (i = 0; str[i] != '\0'; i++)
{
if ((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
{
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' || str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U')
vowels++;
else
consonants++;
}
}
printf("\nNumber of vowels: %d\n", vowels);
printf("Number of consonants: %d\n", consonants);
}
C Program to count number of vowels and consonants in a string using array and function
#include <stdio.h>
int isVowel(char);
void main()
{
char str[50];
int vowels = 0, consonants = 0, i;
printf("Enter string: ");
gets(str);
for (i = 0; str[i] != '\0'; i++)
{
if (isVowel(str[i]))
vowels++;
else
consonants++;
}
printf("\nNumber of vowels: %d\n", vowels);
printf("Number of consonants: %d\n", consonants);
}
int isVowel(char ch)
{
int i = 0;
char vowels[] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
for (i = 0; i < sizeof(vowels); i++)
if (vowels[i] == ch)
return 1;
return 0;
}
Output
Enter string: hello world
Number of vowels: 3
Number of consonants: 7