C program to count vowel and consonant in a string; Through this tutorial, we will learn how to count a number of vowels and consonants in a string using for loop, while loop, ASCII value, function, recursion and pointer in the c program.
Programs to Count Vowels, and Consonants in a String in C
- C program to Count Vowels, and Consonants in a String using For Loop and Ascii Value
- C Program to Count Vowels, and Consonants in a String using Functions
- C Program to Count Vowels, and Consonants in a String using Recursion
- C Program to Count Vowels, and Consonants in a String using While Loop and Pointer
C Program to Count Vowels, and Consonants in a String using For Loop and Ascii Value
#include <stdio.h>
#include <string.h>
int main()
{
char s[1000];
int i,vowels=0,consonants=0;
printf("Enter the string : ");
gets(s);
for(i=0;s[i];i++)
{
if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 && s[i]<=122))
{
if(s[i]=='a'|| s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O' ||s[i]=='U')
vowels++;
else
consonants++;
}
}
printf("vowels = %d\n",vowels);
printf("consonants = %d\n",consonants);
return 0;
}
The output of the above c program; as follows:
Enter the string : hello dear vowels = 4 consonants = 5
C Program to Count Vowels, and Consonants in a String using Functions
#include <string.h>
void stringcount(char *s)
{
int i,vowels=0,consonants=0;
for(i=0;s[i];i++)
{
if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 && s[i]<=122))
{
if(s[i]=='a'|| s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O' ||s[i]=='U')
vowels++;
else
consonants++;
}
}
printf("vowels = %d\n",vowels);
printf("consonants = %d\n",consonants);
}
int main()
{
char s[1000];
printf("Enter the string: ");
gets(s);
stringcount(s);
}
The output of the above c program; as follows:
Enter the string: hello world vowels = 3 consonants = 7
C Program to Count Vowels, and Consonants in a String using Recursion
#include <stdio.h>
#include <string.h>
void stringcount(char *s)
{
static int i,vowels=0,consonants=0;
if(!s[i])
{
printf("vowels = %d\n",vowels);
printf("consonants = %d\n",consonants);
return;
}
else
{
if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 && s[i]<=122))
{
if(s[i]=='a'|| s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O' ||s[i]=='U')
vowels++;
else
consonants++;
}
i++;
stringcount(s);
}
}
int main()
{
char s[1000];
printf("Enter the string: ");
gets(s);
stringcount(s);
}
The output of the above c program; as follows:
Enter the string: Hello temp vowels = 3 consonants = 6
C Program to Count Vowels, and Consonants in a String using While Loop and Pointer
#include <string.h>
int main()
{
char s[1000],*p;
int vowels=0,consonants=0;
printf("Enter the string : ");
gets(s);
p=s;
while(*p)
{
if( (*p>=65 && *p<=90) || (*p>=97 && *p<=122))
{
if(*p=='a'|| *p=='e'||*p=='i'||*p=='o'||*p=='u'||*p=='A'||*p=='E'||*p=='I'||*p=='O' ||*p=='U')
vowels++;
else
consonants++;
}
p++;
}
printf("vowels = %d\n",vowels);
printf("consonants = %d\n",consonants);
return 0;
}
The output of the above c program; as follows:
Enter the string : c program vowels = 2 consonants = 6