#9 | Pointer | Write a program to count vowels and consonants in string using pointer
YouTube Video :-
Question :-
Write a program to count vowels and consonants in string using pointer
Code :-
// Write a program to count vowels and consonants in string using pointer
// vowels = a,e,i,o,u
// consonants = reamining Alphabets from vowels
#include<stdio.h>
void main()
{
// declare some variables
char str[50];
char *s;
int vowels=0,conso=0;
// Collect string from user
printf("Enter Any string :- ");
scanf("%s",str);
// assign memory address
// charcter pointer has value = str[0]
// str's first element that is 0th index's memory address
// is given to the character pointer
s = str;
// count the vowels and consonants
while (*s != '\0')
{
/* code */
if (*s == 'a' || *s == 'e' || *s == 'i' || *s == 'o' || *s == 'u' ||
*s == 'A' || *s == 'E' || *s == 'I' || *s == 'O' || *s == 'U' )
{
vowels++;
}
else
{
conso++;
}
s++;
}
// print the number of vowels and consonnants
printf("\nNumber of Vowels = %d",vowels);
printf("\nNumber of Consonants = %d",conso);
}
Comments
Post a Comment