Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Count the number of vowel, Consonant, number & space

In this example we will count the number of vowel, Consonant, number & space

#include <iostream>
using namespace std;

int main() {
    char line[150];
    int vowel, consonant, number, space;
    vowel = consonant = number = space = 0;

    cout << "Enter a Sentence" << endl;
    cin.getline(line, 150);  // Use cin.getline to read the input, including spaces

    for (int i = 0; line[i] != '\0'; i++) {
        // Check for vowels (both uppercase and lowercase)
        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') {
            ++vowel;
        }
        // Check for consonants (both uppercase and lowercase)
        else if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z')) {
            ++consonant;
        }
        // Check for numbers
        else if (line[i] >= '0' && line[i] <= '9') {
            ++number;
        }
        // Check for spaces
        else if (line[i] == ' ') {
            ++space;
        }
    }

    cout << "Vowel is = " << vowel << endl;
    cout << "Consonant is = " << consonant << endl;
    cout << "Number is = " << number << endl;
    cout << "Space is = " << space << endl;

    return 0;
}

Explanation:

  1. User Input:
    • The program prompts the user to enter a sentence, and the user inputs the string:
Enter a Sentence:
Hello World 123
  • This sentence is stored in the line array.
  1. Initialization:
    • The program initializes the following variables:
      • vowel = 0: To count the number of vowels.
      • consonant = 0: To count the number of consonants.
      • number = 0: To count the digits (numbers).
      • space = 0: To count the number of spaces.
  2. Loop Through Each Character:
    • The for loop iterates over each character in the input string and performs the following checks:
CharacterConditionAction Taken
Helse if (line[i] >= 'A' && line[i] <= 'Z')Consonant count increases by 1
e`if (line[i] == ‘a’
lelse if (line[i] >= 'a' && line[i] <= 'z')Consonant count increases by 1
lelse if (line[i] >= 'a' && line[i] <= 'z')Consonant count increases by 1
o`if (line[i] == ‘a’
else if (line[i] == ' ')Space count increases by 1
Welse if (line[i] >= 'A' && line[i] <= 'Z')Consonant count increases by 1
o`if (line[i] == ‘a’
relse if (line[i] >= 'a' && line[i] <= 'z')Consonant count increases by 1
lelse if (line[i] >= 'a' && line[i] <= 'z')Consonant count increases by 1
delse if (line[i] >= 'a' && line[i] <= 'z')Consonant count increases by 1
else if (line[i] == ' ')Space count increases by 1
1else if (line[i] >= '0' && line[i] <= '9')Number count increases by 1
2else if (line[i] >= '0' && line[i] <= '9')Number count increases by 1
3else if (line[i] >= '0' && line[i] <= '9')Number count increases by 1

4. Final Counts:

  • After the loop finishes, the following values are computed:
    • Vowels: 3 (e, o, o)
    • Consonants: 7 (H, l, l, W, r, l, d)
    • Numbers: 3 (1, 2, 3)
    • Spaces: 2 (between Hello and World, and between World and 123)

5. Output:

  • The program prints the result:
Vowel is = 3
Consonant is = 7
Number is = 3
Space is = 2

    Leave a Reply

    Your email address will not be published.

    Need Help?