VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-to-count-the-number-of-vowels-in-a-word/

⇱ Program to count the number of vowels in a word - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to count the number of vowels in a word

Last Updated : 18 Jan, 2024

Write a program to count the number of vowels in a given word. Vowels include 'a', 'e', 'i', 'o', and 'u' (both uppercase and lowercase). The program should output the count of vowels in the word.

Examples:

Input: "programming"
Output: 3
Explanation: The word "programming" has three vowels ('o', 'a', 'i').

Input: "HELLO"
Output: 2
Explanation: The word "HELLO" has two vowels ('E', 'O').

Approach: To solve the problem, follow the below idea:

Iterate through each character in the given word and check if it is a vowel. If it is, increment the count.

Step-by-step algorithm:

  • Initialize a variable vowelCount to 0 to keep track of the number of vowels.
  • Iterate through each character in the word.
  • Check if the character is a vowel (either uppercase or lowercase).
  • If it is a vowel, increment vowelCount.
  • After iterating through all characters, vowelCount will contain the total count of vowels.

Below is the implementation of the algorithm:


Output
Number of vowels: 3

Time Complexity: O(N), where N is the length of the input word.
Auxiliary Space: O(1)

Comment
Article Tags:
Article Tags: