English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Other

C Language Reference Manual

C program calculates the number of vowels and consonants

Comprehensive Collection of C Programming Examples

In this example, the program counts the vowels, consonants, numbers, and spaces in the user input string.

To understand this example, you should understand the followingC programmingTopic:

The program calculates vowels, consonants, and more

#include <stdio.h>
int main() {
    int vowels, consonant, digit, space;15vowels = 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("Vowel: %d", vowels);
    printf("\nConsonant: %d", consonant);
    printf("\nDigit: %d", digit);
    printf("\nSpace: %d", space);
    return 0;
}

Output result

Enter a line of string: adfslkj34 34lkj343 34lk
Vowel: 1
Consonant: 11
Digit: 9
Space: 2

Here, the string input by the user is stored in the line variable.

Initially, the values of the variables vowel, consonant, digit, and space are initialized to 0.

Then, use a for loop to iterate over the characters of the string. In each iteration, it checks whether the character is a vowel, consonant, digit, and space. If the character is a vowel, in this case, the vowel variable increases1。

When the loop ends, the number of vowels, consonants, digits, and spaces are stored in the variables vowel, consonant,   digit and space.

Comprehensive Collection of C Programming Examples