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

Write a C program to count vowels and consonants in a string using a pointer and a while loop:

  1. We assigned a string to the character pointer.
  2. The if statement checks whether any character equals a, e, i, o, u. If true, vowel count will increment. Otherwise, consonants count incremented.
  3. Finally, this example prints the total vowels and consonants in a string.
#include <stdio.h>

int main()
{
    char str[100];
    char *ch;
    int vowCount = 0, consCount = 0;

    printf("Please Enter String to Count Vowels and Consonants\n");
    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);
}
C program to Count Vowels and Consonants in a String using a Pointer

This c program counts the vowels and consonants in a string using a pointer and for loop. The above example works fine. However, it considers empty spaces as consonants. So, in this example, we used another condition to check the consonants.  

#include <stdio.h>

int main()
{
    char str[100];
    char *ch;
    int vowCount = 0, consCount = 0;

    printf("Please Enter String\n");
    fgets(str, sizeof str, stdin);
  
    for(ch = str; *ch != '\0'; ch++)
    {
        if(*ch == 'a' || *ch == 'e' || *ch == 'i' || *ch == 'o' || *ch == 'u' ||
		*ch == 'A' || *ch == 'E' || *ch == 'I' || *ch == 'O' || *ch == 'U')  
        {
            vowCount++;
        }
        else if((*ch >= 'a' && *ch <= 'z') || (*ch >= 'A' && *ch <= 'Z'))
        {
            consCount++;
        }
    }
    
	printf("Total Vowels     = %d\n", vowCount);
    printf("Total Consonants = %d\n", consCount);
}
Please Enter String
count vowels and consonants example
Total Vowels     = 11
Total Consonants = 20

In this count vowels and consonants in a string using pointer example, the if statement check against the ASCII codes.

#include <stdio.h>

int main()
{
    char str[100];
    char *ch;
    int vowCount = 0, consCount = 0;

    printf("Please Enter String\n");
    fgets(str, sizeof str, stdin);
  
    for(ch = str; *ch != '\0'; ch++)
    {
        if(*ch == 65 || *ch == 69 || *ch == 73 || *ch == 79 || *ch == 85 ||
		*ch == 97 || *ch == 101 || *ch == 105 || *ch == 111 || *ch == 117)  
        {
            vowCount++;
        }
        else if((*ch >= 65 && *ch <= 90) || (*ch >= 97 && *ch <= 122))
        {
            consCount++;
        }
    }
    
	printf("Total Vowels     = %d\n", vowCount);
    printf("Total Consonants = %d\n", consCount);
}
Please Enter String
learn c program in this tutorial gateway website
Total Vowels     = 16
Total Consonants = 25