Program to Find Frequency of Vowel Consonant of String in C
Program to Count Vowels,Consonants,Digits and White Spaces in a String:
We will write a program of vowel consonant frequency string in C language.In this we have to find the frequency or number of times vowels, consonants, digits and white spaces occurred in a string. This will help to understand the basic structure of for loop ,if- else, etc.
Vowel Consonant Frequency String Program:
In this program, we have to check for the following things:
- count the number of vowels in the string
- count the number of consonants in the string
- count the number of digits in the string
- count the number of white spaces in the string
There 21 consonants and 5 vowels (a,e,i,o,u) in English.Apart from this,there are 10 digits from 0 to 9 .
Example:
Input: str = "PrepInsta 1234" Output: Vowels: 3 Consonants: 6 Digits: 4 White Spaces: 1
Problem 1:
Run
#include <stdio.h>
#include<ctype.h>
int main ()
{
char str[150] = "PrepInsta 1234";
int vow, cons, dig, spc;
printf ("Given string %s has \n", str);
vow = cons = dig = spc = 0;
for (int i = 0; str[i] != '\0'; i++)
{
str[i] = tolower (str[i]);
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' ||
str[i] == 'o' || str[i] == 'u')
{
++vow;
}
else if ((str[i] >= 'a' && str[i] <= 'z'))
{
++cons;
}
else if (str[i] >= '0' && str[i] <= '9')
{
++dig;
}
else if (str[i] == ' ')
{
++spc;
}
}
printf ("Vowels: %d", vow);
printf ("\nConsonants: %d", cons);
printf ("\nDigits: %d", dig);
printf ("\nWhite spaces: %d", spc);
return 0;
}
Output
Given string PrepInsta 1234 has Vowels: 3 Consonants: 6 Digits: 4 White spaces: 1
NOTE:
We have used the tolower( ) inbuilt function to convert all the characters whether capital or small to lowercase.So, to use this function we must import ctype.h header file.
Problem 2: Using Function
Run
#include<stdio.h>
int isVow (char ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'
|| ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
{
return 1;
}
else
{
return 0;
}
}
int isCons (char ch)
{
if (((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) && !isVow (ch))
{
return 1;
}
else
{
return 0;
}
}
int isDig (char ch)
{
if (ch >= '0' && ch <= '9')
{
return 1;
}
else
{
return 0;
}
}
int isspc (char ch)
{
if (ch == ' ')
{
return 1;
}
else
{
return 0;
}
}
int main ()
{
char str[500] = "PrepInsta 1234";
printf ("Given string %s has \n", str);
int V = 0, C = 0, D = 0, W = 0, i;
for (i = 0; str[i] != '\0'; i++)
{
V += isVow (str[i]);
C += isCons (str[i]);
D += isDig (str[i]);
W += isspc (str[i]);
}
printf ("Vowels: %d\n",V);
printf ("Consonants: %d\n",C);
printf ("Digits: %d\n",D);
printf ("White spaces: %d",W);
return 0;
}
Output
Given string PrepInsta 1234 has Vowels: 3 Consonants: 6 Digits: 4 White spaces: 1
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Login/Signup to comment