C Program to Count Vowels and Consonants in a String using a Pointer

In this tutorial, i am going to show you how to count vowels and consonants in a string using a pointer in c.

C Program to Count Vowels and Consonants in a String using a Pointer

#include <stdio.h>
int main()
{
    char str[100];
    char *ch;
    int vowCount = 0, consCount = 0;
    printf("Please Enter String to Count Vowels and Consonants :- ");
    fgets(str, sizeof str, stdin);
    ch = str;
    
    while(*ch != '\0')
    {
        if(*ch == 'a' || *ch == 'e' || *ch == 'i' || *ch == 'o' || *ch == 'u' ||
		*ch == 'A' || *ch == 'E' || *ch == 'I' || *ch == 'O' || *ch == 'U')  
        {
            vowCount++;
        }
        else
        {
            consCount++;
        }
        ch++;
    }
    
	printf("Total Vowels     = %d\n", vowCount);
    printf("Total Consonants = %d\n", consCount - 1);
}

The result of the above c program; is as follows:

Please Enter String to Count Vowels and Consonants :- laratutorials.com
Total Vowels     = 4
Total Consonants = 8

More C Programming Tutorials

Leave a Comment