Menu
Coddy logo textTech

Counting Vowels

Part of the Logic & Flow section of Coddy's C journey — lesson 22 of 63.

challenge icon

Challenge

Easy

Extend your text utility program from the previous lesson to add vowel counting functionality. Your program should:

  1. Prompt the user to enter a short sentence by printing: Enter a sentence:
  2. Declare a character array named sentence with 200 elements to store the input
  3. Use scanf with the %s format specifier to read a single word from the user
  4. Print the stored word in the format: You entered: [word]
  5. Use strlen() to calculate the total number of characters in the word
  6. Print the character count in the format: Character count: [count]
  7. Print the length using strlen() in the format: Length: [length]
  8. Iterate through each character in the word using a loop
  9. For each character, check if it is a vowel (a, e, i, o, u) in both lowercase and uppercase
  10. Keep a running count of vowels found
  11. Print the vowel count in the format: Vowel count: [count]

Remember to include the <string.h> header to use the strlen() function.

Your output should display the results in the following format:

Enter a sentence: 
You entered: [word]
Character count: [count]
Length: [length]
Vowel count: [count]

For example, if the input is:

Programming

Your output should be:

Enter a sentence: 
You entered: Programming
Character count: 11
Length: 11
Vowel count: 3

This challenge builds upon the previous lesson by adding vowel counting functionality. You will need to use a loop to iterate through each character in the string and use conditional statements to check if each character matches any of the vowel characters (both uppercase and lowercase versions).

Try it yourself

#include <stdio.h>
#include <string.h>

int main() {
    char sentence[200];
    
    printf("Enter a sentence: \n");
    scanf("%s", sentence);
    
    printf("You entered: %s\n", sentence);
    printf("Character count: %d\n", (int)strlen(sentence));
    printf("Length: %d\n", (int)strlen(sentence));
    
    return 0;
}

All lessons in Logic & Flow