Solved programming problem

Count the number of vowels and consonants in a string using a pointer in C.

Source code

#include <stdio.h>

int count(char *ptr)

{

    int i=0,vowel=0,consonant=0;

    while(*(ptr+i)!='')

    {

        if(*(ptr+i)=='a' ||*(ptr+i)=='e' ||*(ptr+i)=='i' ||*(ptr+i)=='o' ||*(ptr+i)=='u')

        {

            vowel++;

            i++;

        }

        else  if  (*(ptr+i)==' ')

        {

            i++;

        }

        else

        {

            consonant++;

            i++;

        }

    }

    printf("
Vowels: %d",vowel);

    printf("
Consonants: %d
",consonant);

}

int main()

{

    char str[100];

    char *ptr;

    printf("Please Enter a massage: ");

    gets(str);

    ptr=str;

    count(ptr);

    return 0;

}

Output

Please Enter a massage: code to haven

Vowels: 5
Consonants: 6

Join the discussion

Add your comment