C program to count total number of vowels and consonants in a string

Write a C program to find total number of vowels and consonants in a string using loop and if else. How to find total number of vowels and consonants in a string using switch case in C programming. Logic to count number of vowels and consonants in a string.

Example

Input

Input string: I love Codeforwin.

Output

Total Vowels = 7
Total Consonants = 8

Required knowledge

Basic C programming, If else, For loop, String

Must know - Program to check vowel and consonant

Logic to count number of vowels and consonants

Below is the step by step descriptive logic to count number of vowels and consonants in a string.

  1. Input string from user, store it in some variable say str.
  2. Initialize two other variables to store vowel and consonant count. Say vowel = 0 and consonant = 0.
  3. Run a loop from start till end of string.
  4. Inside the loop increment vowel by 1 if current character is vowel. Otherwise increment consonant by 1 if current character is consonant.

Program to count number of vowels and consonants using if

/**
 * C program to count total number of vowel or consonant in a string
 */

#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum string size

int main()
{
    char str[MAX_SIZE];
    int i, len, vowel, consonant;

    /* Input string from user */
    printf("Enter any string: ");
    gets(str);

    vowel = 0;
    consonant = 0;
    len = strlen(str);

    for(i=0; i<len; i++)
    {
        if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
        {
            /*
             * If the current character(str[i]) is a vowel
             */
            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++;
            else
                consonant++;
        }
    }

    printf("Total number of vowel = %d\n", vowel);
    printf("Total number of consonant = %d\n", consonant);

    return 0;
}

The above approach to count number of vowels and consonants in a string is easy to understand for every beginner. However, for this program use of switch case is recommended instead of if.

Program to count number of vowels and consonants using switch case

/**
 * C program to count total number of vowel or consonant in a string using switch case
 */

#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum string size

int main()
{
    char str[MAX_SIZE]; 
    int i, len, vowel, consonant;

    /* Input strings from user */
    printf("Enter any string: ");
    gets(str);


    vowel = 0;
    consonant = 0;
    len = strlen(str);

    for(i=0; i<len; i++)
    {
        if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
        {
            switch(str[i])
            {
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                    vowel++;
                    break;
                default:
                    consonant++;
            }
        }
    }

    printf("Total number of vowel = %d\n", vowel);
    printf("Total number of consonant = %d\n", consonant);

    return 0;
}

Finally you can re-write the program using pointers.

Program to count number of vowels and consonants using pointers

/**
 * C program to count total number of vowel or consonant in a string using pointers
 */

#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum string size

int main()
{
    char str[MAX_SIZE]; 
    char * s = str;
    int vowel, consonant;

    /* Input strings from user */
    printf("Enter any string: ");
    gets(str);

    vowel = 0;
    consonant = 0;

    while(*s)
    {
        if((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <='Z'))
        {
            switch(*s)
            {
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                    vowel++;
                    break;
                default:
                    consonant++;
            }
        }
        s++;
    }

    printf("Total number of vowel = %d\n", vowel);
    printf("Total number of consonant = %d\n", consonant);

    return 0;
}

Output

Enter any string: I love Codeforwin!
Total number of vowel = 7
Total number of consonant = 8

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>