Vowel Counter

Vowel Counter

Medium C Strings 36 views
Explanation Complexity

Problem Statement

Count the vowels (a, i, i, o, u) in the string. Case-insensitively. Also keep a separate count of each vowel. You can create a histogram.

Input Format

A string entered by the user.

Output Format

Total vowel count
AND
Count of each vowel (a, e, i, o, u).

Example

"Education"
Total vowels = 5
a : 1
e : 1
i : 1
o : 1
u : 1

Constraints

• Case-insensitive (A and a are same)

• Only vowels: a, e, i, o, u

Concept Explanation

Each character of the string is checked.
If it is a vowel (in any case), its count is increased in a histogram.

Step-by-Step Explanation

1.Create an integer array vowelCount[5] initialized to 0.

• Index 0 → a

• Index 1 → e

• Index 2 → i

• Index 3 → o

• Index 4 → u
2.Traverse the string character by character.

3.Convert each character to lowercase.

4.Check the character:

• If a → increment vowelCount[0]

• If e → increment vowelCount[1]

• If i → increment vowelCount[2]

• If o → increment vowelCount[3]

• If u → increment vowelCount[4]
5.After traversal, calculate total vowels by adding all counts.

6.Print total vowel count.

7.Print only vowels whose count is greater than 0

Concept Explanation

Each character of the string is checked.
If it is a vowel (in any case), its count is increased in a histogram.

Step-by-Step Explanation

1.Create an integer array vowelCount[5] initialized to 0.

• Index 0 → a

• Index 1 → e

• Index 2 → i

• Index 3 → o

• Index 4 → u
2.Traverse the string character by character.

3.Convert each character to lowercase.

4.Check the character:

• If a → increment vowelCount[0]

• If e → increment vowelCount[1]

• If i → increment vowelCount[2]

• If o → increment vowelCount[3]

• If u → increment vowelCount[4]
5.After traversal, calculate total vowels by adding all counts.

6.Print total vowel count.

7.Print only vowels whose count is greater than 0

Input / Output Format

Input Format
A string entered by the user.
Output Format
Total vowel count
AND
Count of each vowel (a, e, i, o, u).
Constraints
• Case-insensitive (A and a are same)

• Only vowels: a, e, i, o, u

Examples

Input:
"Education"
Output:
Total vowels = 5 a : 1 e : 1 i : 1 o : 1 u : 1

Example Solution (Public)

C
#include <stdio.h>
#include <ctype.h>

int main() {
    char str[200];
    int a = 0, e = 0, i = 0, o = 0, u = 0;
    int total = 0;
    int idx;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    for (idx = 0; str[idx] != '�'; idx++) {
        char ch = tolower(str[idx]);

        switch (ch) {
            case 'a': a++; total++; break;
            case 'e': e++; total++; break;
            case 'i': i++; total++; break;
            case 'o': o++; total++; break;
            case 'u': u++; total++; break;
        }
    }

    printf("nVowel counts:n");
    printf("a: %dn", a);
    printf("e: %dn", e);
    printf("i: %dn", i);
    printf("o: %dn", o);
    printf("u: %dn", u);
    printf("Total vowels: %dn", total);

    // Histogram
    printf("nHistogram:n");

    printf("a | ");
    for (idx = 0; idx < a; idx++) printf("*");
    printf("n");

    printf("e | ");
    for (idx = 0; idx < e; idx++) printf("*");
    printf("n");

    printf("i | ");
    for (idx = 0; idx < i; idx++) printf("*");
    printf("n");

    printf("o | ");
    for (idx = 0; idx < o; idx++) printf("*");
    printf("n");

    printf("u | ");
    for (idx = 0; idx < u; idx++) printf("*");
    printf("n");

    return 0;
}

Official Solution Code

#include <stdio.h>
#include <ctype.h>

int main() {
    char str[200];
    int a = 0, e = 0, i = 0, o = 0, u = 0;
    int total = 0;
    int idx;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    for (idx = 0; str[idx] != '�'; idx++) {
        char ch = tolower(str[idx]);

        switch (ch) {
            case 'a': a++; total++; break;
            case 'e': e++; total++; break;
            case 'i': i++; total++; break;
            case 'o': o++; total++; break;
            case 'u': u++; total++; break;
        }
    }

    printf("nVowel counts:n");
    printf("a: %dn", a);
    printf("e: %dn", e);
    printf("i: %dn", i);
    printf("o: %dn", o);
    printf("u: %dn", u);
    printf("Total vowels: %dn", total);

    // Histogram
    printf("nHistogram:n");

    printf("a | ");
    for (idx = 0; idx < a; idx++) printf("*");
    printf("n");

    printf("e | ");
    for (idx = 0; idx < e; idx++) printf("*");
    printf("n");

    printf("i | ");
    for (idx = 0; idx < i; idx++) printf("*");
    printf("n");

    printf("o | ");
    for (idx = 0; idx < o; idx++) printf("*");
    printf("n");

    printf("u | ");
    for (idx = 0; idx < u; idx++) printf("*");
    printf("n");

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.