VOWEL,CONSONANT,DIGIT AND SPACE in a string in C language

 C PROGRAM TO FIND VOWEL,CONSONANT,DIGIT AND SPACE

code:

#include <stdio.h>

int main()

 {

    char line[150];

    int vowels, consonant, digit, space;

    vowels = consonant = digit = space = 0;

    printf("Enter a line of string: ");

    fgets(line, sizeof(line), stdin);

    for (int i = 0; line[i] != '\0'; ++i) 

{

        if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' ||

            line[i] == 'o' || line[i] == 'u' || line[i] == 'A' ||

            line[i] == 'E' || line[i] == 'I' || line[i] == 'O' ||

            line[i] == 'U') {

            ++vowels;

        } else if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z'))

{

            ++consonant;

        } else if (line[i] >= '0' && line[i] <= '9') 

{

            ++digit;

        } else if (line[i] == ' ') 

{

            ++space;

        }

    }

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

    printf("\nConsonants: %d", consonant);

    printf("\nDigits: %d", digit);

    printf("\nWhite spaces: %d", space);

    return 0;

}


Explanation:

  • First we are declaring the variables
  • The second is that we entering a string, so that we can find the vowel, space and consonant
  • Next we using if statement to check whether the string has vowel
  • And alternatively we are using if statement to check for number, space and consonant.

Output 1:




Output 2:








Comments

Popular posts from this blog

Use of Backslash "\n" in C language

COHESION AND COUPLING material

Coding and Testing in software engineering