Count Vowels in Java: Explanation & Practice

Count vowels in a string

Problem summary

Write a program that counts all vowels (a, e, i, o, u) in a string.

Starter code

public class Main {
    public static void main(String[] args) {
        String text = "Hello World"; // Test case 1
        
        // Count vowels (case insensitive)
        // Print: Vowel count: <result>
    }
}

Expected output and test cases

  • e, o, o in Hello World
    Vowel count: 3
  • All vowels once
    Vowel count: 5
  • No vowels
    Vowel count: 0

Hints

  1. Check each character against vowels
  2. Use toLowerCase() for case insensitive
  3. Can use indexOf or contains on vowel string

Related Strings exercises

Practice all Strings exercises · Run this idea in the Java compiler