C program to count the number of vowels in a string

C program to count the number of vowels in a string.

In this article we will learn a simple C program to count the number of vowels in a string.
‘A’, ‘E’, ‘I’, ‘O’, ‘U’ are five vowels out of 26 characters in English alphabet letters.C programming is case sensitive, and hence lowercase and uppercase characters are considered differently, so we will have to check for both the cases.

C program to count number of vowels in a string.

Algorithm:

  • Initialize the variable.
  • Accept the input.
  • Initialize for loop.
  • Check and count the vowels.
  • Terminate for loop.
  • Print total count.

C programming code to count the number of vowels in a string.

Competitive Coding Techniques
#include  <stdio.h>
#include  <string.h>
 
int main()
{
    //Initializing variable.
    char str[100];  
    int i,vowels=0;
    
    //Accepting input.
    printf(" Enter  the string : ");
    gets(str);
    
    //Initializing for loop. 
    for(i=0;str[i];i++)  
    {
        //Counting the vowels.
        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++;
        }
    }
 	
    //Printing the count of vowels.
    printf(" Total number of vowels in the string = %d\n",vowels);
    
    
    return 0;
}

Output:
Enter  the string : PREPINSTA
Total number of vowels in the string = 3