C program to count vowels and consonants in a string using pointer

By IncludeHelp Last updated : March 10, 2024

In this C program, we are counting the total number of vowels and consonants of a string using pointer.

Counting vowels and consonants in a string using pointer

Here, we are reading a string and assigning its base address to the character pointer, and to check characters are vowels or consonants, we will check and count each character one by one by increasing the pointer.

C program to count vowels and consonants in a string using pointer

/*C program to count vowels and consonants in a string using pointer.*/
#include <stdio.h>
int main()
{
    char str[100];
    char *ptr;
    int  cntV,cntC;
     
    printf("Enter a string: ");
    gets(str);
     
    //assign address of str to ptr
    ptr=str;
     
    cntV=cntC=0;
    while(*ptr!='\0')
    {
        if(*ptr=='A' ||*ptr=='E' ||*ptr=='I' ||*ptr=='O' ||*ptr=='U' ||*ptr=='a' ||*ptr=='e' ||*ptr=='i' ||*ptr=='o' ||*ptr=='u')
            cntV++;
        else
            cntC++;
        //increase the pointer, to point next character
        ptr++;
    }
     
    printf("Total number of VOWELS: %d, CONSONANT: %d\n",cntV,cntC);        
    return 0;
}

Output

Enter a string: This is a test string
Total number of VOWELS: 5, CONSONANT: 16

C Pointer Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.