Count Vowels in String C Program

By | October 12, 2016

Learn How To Find and Count Vowels in String in C Programming Language. The Number of Vowels in a String can be count using If Else, Switch Case and Pointers as well.

What is Vowel?

A letter representing a vowel sound, such as a, e, i, o, u.

Must Read: C Program To Print Vowels in a String

C Program To Count Vowels in String using If Else

#include<stdio.h>
#include<string.h>

int main()
{
      int count = 0, i, length;
      char ch, str[30];
      printf("\nEnter A String:\t");
      scanf("%[^\n]s", str);
      for(i = 0; i < strlen(str); i++)
      {
            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')
            {
                  count++;
            }
      }
      printf("\nTotal Number of Vowels:\t%d\n", count);
      return 0;
}

Must Read: C Program To Replace A Character in a String

C Program To Count Number of Vowels in a String using Switch Case

#include<stdio.h>
#include<string.h>

int main()
{
      int count, sum = 0, length;
      char ch, str[30];
      int a = 0, e = 0, i = 0, o = 0, u = 0, A = 0, E = 0, I = 0, O = 0, U = 0;
      printf("\nEnter A String:\t");
      scanf("%[^\n]s", str);
      length = strlen(str);
      for(count = 0; count < length; count++)
      {
            ch = str[count];
            switch(ch)
            {
                  case 'a': a++; sum++; break;
                  case 'e': e++; sum++; break;
                  case 'i': i++; sum++; break;
                  case 'o': o++; sum++; break;
                  case 'u': u++; sum++; break;
                  case 'A': A++; sum++; break;
                  case 'E': E++; sum++; break;
                  case 'I': I++; sum++; break;
                  case 'O': O++; sum++; break;
                  case 'U': U++; sum++; break;
            }
      }
      printf("\nTotal Number of Vowels:\t%d\n", sum);
      printf("\nCount of Vowel: a = %d", a);
      printf("\nCount of Vowel: e = %d", e);
      printf("\nCount of Vowel: i = %d", i);
      printf("\nCount of Vowel: o = %d", o);
      printf("\nCount of Vowel: u = %d", u);
      printf("\nCount of Vowel: A = %d", A);
      printf("\nCount of Vowel: E = %d", E);
      printf("\nCount of Vowel: I = %d", I);
      printf("\nCount of Vowel: O = %d", O);
      printf("\nCount of Vowel: U = %d\n", U);
      return 0;
}

Must Read: C Program To Delete Vowels from String

 

C Program To Count Vowels in String using Pointers

#include<stdio.h>
#include<string.h>

int main()
{
      int i, sum = 0;
      char str[30], *ptr;
      printf("\nEnter A String:\t");
      scanf("%[^\n]s", str);
      ptr = str;
      while(*ptr != '\0')
      {
            if(*ptr == 'a' || *ptr == 'e' || *ptr == 'i' || *ptr == 'o' || *ptr == 'u' || *ptr == 'A' || *ptr == 'E' || *ptr == 'I' || *ptr == 'O' || *ptr == 'U')
            {
                  sum++;
            }
            ptr++;
      }
      printf("\nTotal Number of Vowels:\t%d\n", sum);
      return 0;
}

Output

C Program To Count Vowels in String using If Else and Swtch Case

If you have any doubts or compilation errors in this c code to print and count the number of vowels and consonants in a string, let us know about it in the comment section below.

Let's Discuss