In this tutorial you will learn about the C Program To Count number of vowels in a string and its application with practical example.
In this tutorial, we will learn to create a C program that will find the number of vowels in a string using C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Basic input/output.
- loop statements.
- for Loop.
- Array.
What Is vowel ?
Let us take an example, in the following string “Education” there are five vowels ‘a’,’e’,’i’,’o’ and ‘u’. here in this program we check every character inputted by user in a string and find the number of vowel in it.
C Program To Count number of vowels in a string.
First of all In this program we take string from user,and with the help of array and loop we will find number of vowel in given string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> void main() { char str[100]; int len,i,vowel=0; printf("\nENTER A STRING: "); gets(str); // getting string from user len=strlen(str); // finding length of a string for(i=0;i<len;i++) { // checking vowels in a string 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') vowel=vowel+1; } printf("\nThere are %d Vowels ian a given String",vowel); //printing number of vowels } |
Output
In the above program, we have first declared and initialized a set variables required in the program.
- str = number of terms to be printed
- len = counting length of string
- vowel= for counting number of vowel
- i= for iteration of loop.
Explanation.
In this program we will do following task step by step.
- First of all we will take string as input.
- then with help of strlen() function we find the length of a string.
- Then check each character of the string to check as shown in above image.
- If the string character is a vowel, increment the counter value of vowels.
- Print the total number of vowels in the end.