recent posts

Check vowel and consonants | C programming


Hi, I'm Golam Rabbani. Today I will discuss about vowel and consonants. This is C programming tutorial. Greetings! How are you all? Hope everyone is well.

C program to check whether a character is vowel or consonant.


Question 1: Check vowel and consonants using if else.
#include <stdio.h>
int main(){
    char ch;
    printf("Enter a character: ");
    scanf("%c", &ch);
    // Checking both lower and upper case
    if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U'){
        printf("%c is a vowel.\n", ch);
    }
    else{
        printf("%c is a consonant.\n", ch);
    }
    return 0;
}
Question 2: Check vowel and consonants using switch statement.
#include <stdio.h>
int main(){
    char ch;
    printf("Input a character: ");
    scanf("%c", &ch);
    switch(ch){
        case 'a':
        case 'A':
        case 'e':
        case 'E':
        case 'i':
        case 'I':
        case 'o':
        case 'O':
        case 'u':
        case 'U':
            printf("%c is a vowel.\n", ch);
            break;
        default:
            printf("%c is a consonant.\n", ch);
    }
    return 0;
}
Question 3: Write a program to count vowels and consonants.
#include <stdio.h>
int main(){
    int i,vcount=0,ccount=0,length=0;
    char ch[50]; //character length
    printf("Input a character: ");
    gets(ch);
    for(i=0; ch[i]!='\0'; i++){
        // ch[i]!='\0' means, loop continue.
        //if character are null then it stop.
        if(ch[i] == 'a' || ch[i] == 'A' || ch[i] == 'e' || ch[i] == 'E' || ch[i] == 'i' || ch[i] == 'I' || ch[i] =='o' || ch[i]=='O' || ch[i] == 'u' || ch[i] == 'U')
        {
            vcount++;
        }
        else if(ch[i] == ' ' || ch[i] == ',' || ch[i] == '.')
        {
        //this condition avoid only space,comma,full stop.
        //you can more validation.
        }
        else{
            ccount++;
        }
        length++;

    }
    printf("Total vowel = %d\n", vcount);
    printf("Total consonants = %d\n", ccount);
    printf("Total character length = %d\n", length);
    return 0;
}
Output:


Check vowel and consonants | C programming Check vowel and consonants | C programming Reviewed by Golam Rabbani on November 21, 2018 Rating: 5

No comments:

top navigation

Powered by Blogger.