Written by Sai Sameera Thota on July 24, 2013 in C > Programming

C Program to Find Number of Vowels present in a String

GET ALERTS:

Get our Latest updates delivered to your mailbox!

In the program, a string or a sentence is given as an input and the number of vowels and consonants in the given statement are displayed as output.

The program uses gets() to read the string. The for loop iterates until the value of i equals the length of the string. The length of the string is calculated using the function strlen(). Each character is converted into a lower case letter using the string function tolower() so that the uppercase letters can also be counted. In the for loop each character of the string is checked if it matches with the vowels i.e, a,e,i,o,u. If the character matches with any of the vowels, the variable vow will be incremented. If the character is not a vowel then it can be considered as a consonant and the value of con increments. After checking all the characters, the number of vowels and consonats are displayed as an output.


#include<stdio.h>
#include<string.h>
main()
{
	int i,vow=0,con=0;
	char arr[50];
	printf("\nEnter a string : ");
	gets(arr);
	for(i=0;i<strlen(arr);i++)
	{
		arr[i] = tolower(arr[i]);
		if((int)arr[i]>=97 && (int)arr[i]<=122)
		{
		   if(arr[i]=='a'||arr[i]=='e'||arr[i]=='i'||arr[i]=='o'||arr[i]=='u')
		   vow++;
		   else
		   con++;
	    }
		
		
	}
	printf("vowels = %d\nconsonants = %d",vow,con);
}

Output:

number_of_vowels

{ 1 comment… read it below or add one }

liza October 3, 2013 at 3:10 pm

Thank you..it help me alot

Reply

Leave a Comment

Previous post:

Next post: