Signup/Sign In

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:

  1. Start
  2. Declare the string
  3. Ask the user to initialize the string.
  4. Declare a pointer variable.
  5. Assign the pointer to the string.
  6. Using a while loop check for each character till the end of the string is reached.
  7. If a vowel is found increment the vowel count.
  8. If a consonant is found increment the consonant count.
  9. Display the result.
  10. 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:

  1. Start
  2. Declare the string
  3. Ask the user to initialize the string.
  4. Call the function that will calculate the number of vowels and consonants.
  5. Declare a pointer variable.
  6. Assign the pointer to the string.
  7. Using a while loop check for each character till the end of the string is reached.
  8. If a vowel is found increment the vowel count.
  9. If a consonant is found increment the consonant count.
  10. Display the result.
  11. 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