Q:

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

belongs to collection: C pointers example programs

0

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

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.*/
#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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C program to print a string character by character... >>
<< C program to read array elements and print the val...