Program to count vowels and consonants in a string using a pointer in c; Through this tutorial, we will learn 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 output of the above c program; is as follows:
Please Enter String to Count Vowels and Consonants :- Tutsmake.com Total Vowels = 4 Total Consonants = 8