Using Pointers Count Vowels – Write a program in C Programming Language to count the number of vowels and consonants in a string using a pointer.
Source Code for Program
#include <stdio.h> int main() { char str1[100],ch; char *ptr; int vcount=0,ccount=0; printf("\n\n Using C Pointers : Count Vowels and Consonants :\n"); printf("-----------------------------------------------------\n"); printf(" Enter a string: "); gets(str1); /*store base address of str1 in to pointer ptr */ ptr=str1; /* loop to iterate through the string characters */ while(*ptr!='\0') { /*convert character to lowercase*/ ch = tolower(*ptr); /* check and count vowels or consonants */ if(ch=='a' ||ch=='e' ||ch=='i' ||ch=='o' ||ch=='u') vcount++; else if ( ch>='a' && ch<='z') /*if it is an alphabet and not vowel*/ ccount++; ptr++; /*point to next character */ } printf(" Number of vowels : %d\n Number of consonants : %d\n",vcount,ccount); return 0; }
Output
Using C Pointers : Count Vowels and Consonants :
—————————————————–
Enter a string: abc def
Number of vowels : 2
Number of consonants : 4