Count the number of vowels and consonants in a string using pointers
In this tutorial, we will learn how to count the total number of vowels and consonants in a string using pointers. But before moving forward, if you are not familiar with the concept of Pointers in C then, do check the article Pointers in C.
Here, we are given a string and our task is to count the number of vowels and consonants using pointers
Input: Enter the String: Hello World
Output: Number of Vowels is 3
Number of consonants is 7
This problem can be solved in two ways:
Method 1: Without using functions.
Method 2: Using Functions.
Let us look at each of these methods separately.
Program 1: Count the number of Vowels and Consonants
In this method, the total number of vowels and consonants are calculated using pointers. This is done in the main method itself.
Algorithm:
- Start
- Declare the string
- Ask the user to initialize the string.
- Declare a pointer variable.
- Assign the pointer to the string.
- Using a while loop check for each character till the end of the string is reached.
- If a vowel is found increment the vowel count.
- If a consonant is found increment the consonant count.
- Display the result.
- Stop.
Below is the code for the same.
In this method, firstly we will declare a string and ask the user to initialize the array. Here, we will directly calculate the total number of vowels and consonants using pointers in the driver program itself. In order to do this, we will use a while loop which will calculate the same.
#include <stdio.h>
int main()
{
char str[150]; //Declare a string
char *p; //Declare a pointer
int vCnt=0,cCnt=0;
printf("Enter the string: ");
fgets(str, 150, stdin); //Initialize the string
p=str;
while(*p!='\0')
{
if(*p=='A' ||*p=='E' ||*p=='I' ||*p=='O' ||*p=='U'
||*p=='a' ||*p=='e' ||*p=='i' ||*p=='o' ||*p=='u')
vCnt++; //Increment vowel count
else
cCnt++; //Increment consonant count
p++;
}
printf("Number of Vowels in String: %d\n",vCnt); //Print the number of vowels
printf("Number of Consonants in String: %d",cCnt); //Print the number of consonants
return 0;
}
Enter the string: hello world
Vowels: 3
Consonants: 7
Program 2: Count the number of Vowels and Consonants
In this method, we calculate the total number of vowels and consonants using functions. Here, we will call another function that will calculate the number of vowels and consonants and print the values.
Algorithm:
- Start
- Declare the string
- Ask the user to initialize the string.
- Call the function that will calculate the number of vowels and consonants.
- Declare a pointer variable.
- Assign the pointer to the string.
- Using a while loop check for each character till the end of the string is reached.
- If a vowel is found increment the vowel count.
- If a consonant is found increment the consonant count.
- Display the result.
- Stop.
Below is the code for the same.
In this method, firstly we will declare a string and ask the user to initialize the array. Here, we will call a function to calculate the total number of vowels and consonants using pointers. In order to do this, we will use a while loop which will calculate the same.
#include <stdio.h>
void Count(char str[150]) //Function Definition
{
int vCnt=0,cCnt=0;
char *p=str;
while(*p!='\0')
{
if(*p=='A' ||*p=='E' ||*p=='I' ||*p=='O' ||*p=='U'
||*p=='a' ||*p=='e' ||*p=='i' ||*p=='o' ||*p=='u')
vCnt++; //Incremenet vowel count
else
cCnt++; //Incremenet consonant count
p++;
}
printf("Number of Vowels in String: %d\n",vCnt);
printf("Number of Consonants in String: %d",cCnt-1);
}
int main()
{
char str[150]; //String Declaration
printf("Enter the string: ");
fgets(str, 150, stdin);
Count(str); //Function Call
return 0;
}
Enter the string: empower
Number of Vowels in String: 3
Number of Consonants in String: 4