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.

Example:
Input text: I love CodeforWin!
Total Vowels = 7
Total Consonants = 8

Required knowledge

Basic C programming, If else, For loop, String

Logic to count number of vowels and consonants

Logic to count total number of vowels/consonants uses a basic concept. What we need to do is we need to traverse entire string character by character and increment the vowel counter if the current character is vowel otherwise increment consonant counter. Therefore before counting total number of vowels and consonants in a string we must know how to check whether the current character is vowel or consonant.
Algorithm to count total number of vowels and consonants in a string
%%Input : text {Array of characters /String}
        N {Size of the string}
Begin:
    vowel ← 0;
    consonant ← 0;
    For i ← 0 to N do
        If (text[i] == 'a','e','i','o','u','A','E','I','O','U') then
            vowelvowel + 1;
        End if
        Else if (text[i] >='a' and <='z') or (text[i] >='A' and <='Z') then
            consonantconsonant + 1;
        End if
    End for
End 


There are various ways you can do this program. I am writing two of them.

Program to count number of vowels and consonants

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

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

#define MAX_SIZE 100 //Maximum size of the string

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

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

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

    for(i=0; i<len; i++)
    {
        /*
         * If the current character(string[i]) is a vowel both upper and lowercase characters
         */
        if(string[i] =='a' || string[i]=='e' || string[i]=='i' || string[i]=='o' || string[i]=='u' || string[i]=='A' || string[i]=='E' || string[i]=='I' || string[i]=='O' || string[i]=='U')
        {
            vowel++;
        }
        else if((string[i]>='a' && string[i]<='z') || (string[i]>='A' && string[i]<='Z'))
        {
            consonant++;
        }
    }

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

    return 0;
}


Program to count number of vowels and consonants using switch

/**
 * 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 size of the string

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

    /* Reads a strings from user */
    printf("Enter any string: ");
    gets(string);


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

    for(i=0; i<len; i++)
    {
        /* If the current character(string[i]) is a an alphabet */
        switch(string[i])
        {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
                vowel++;
                break;
            default:
                if((string[i]>='a' && string[i]<='z') || (string[i]>='A' && string[i]<='Z'))
                {
                    consonant++;
                }
        }
    }

    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 ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment