Count Vowels

This program counts how many vowels are present in a string.
It’s useful for string manipulation and logic testing in interviews.

Java Code
// Java program to count vowels in a string
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a string:");
        String input = scanner.nextLine().toLowerCase(); // convert to lowercase

        int count = 0;

        // loop through characters and check vowels
        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);

            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                count++;
            }
        }

        System.out.println("Number of vowels: " + count);
    }
}
    
Output
Enter a string:
Number of vowels: 5