w3resource

Java Project - Vowel Counter


Java - Vowel Counter using Loops and If-Else:

Vowel Counter :

Count the number of vowels in a given string.

A simple program that counts the number of vowels (a, e, i, o, u) in a given string. It processes the string and returns the total number of vowels found.

Input: A string.
Output: Number of vowels in the string.

Example:

  • Input: "hello"
  • Output: 2

Solution 1: Vowel Counter using Loops and If-Else

Code:

import java.util.Scanner;

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

        // Taking user input for the string
        System.out.print("Enter a string: ");
        String input = scanner.nextLine();

        // Convert the input string to lowercase to handle both uppercase and lowercase vowels
        input = input.toLowerCase();

        int vowelCount = 0;

        // Loop through the string and check for vowels
        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);
            // If the current character is a vowel, increment the count
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                vowelCount++;
            }
        }

        // Display the result
        System.out.println("Number of vowels: " + vowelCount);

        scanner.close(); // Close the scanner resource
    }
}

Output:

Enter a string:  Exercises
Number of vowels: 4
Enter a string:  Loops 
Number of vowels: 2

Explanation :

  • Input: The program prompts the user to enter a string.
  • Convert to Lowercase: Converts the string to lowercase to handle both uppercase and lowercase vowels.
  • Loop through String: Loops through each character of the string.
  • Vowel Check: Uses an if-else block to check if the current character is a vowel.
  • Count Vowels: If the character is a vowel, it increments the vowel count.
  • Output: Displays the total number of vowels in the string.
  • Close Scanner: The scanner is closed to free up system resources.

Solution 2: Vowel Counter using Regular Expressions (Regex)

Code:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

        // Taking user input for the string
        System.out.print("Enter a string: ");
        String input = scanner.nextLine();

        // Convert the input string to lowercase to handle both uppercase and lowercase vowels
        input = input.toLowerCase();

        // Define a regular expression pattern for vowels
        Pattern vowelPattern = Pattern.compile("[aeiou]");
        Matcher matcher = vowelPattern.matcher(input);

        int vowelCount = 0;

        // Count vowels using the regular expression matcher
        while (matcher.find()) {
            vowelCount++;
        }

        // Display the result
        System.out.println("Number of vowels: " + vowelCount);

        scanner.close(); // Close the scanner resource
    }
}  

Output:

Enter a string:  Counter
Number of vowels: 3
Enter a string:  Cry
Number of vowels: 0

Explanation:

  • Input: The program prompts the user to enter a string.
  • Convert to Lowercase: Converts the string to lowercase to handle both uppercase and lowercase vowels.
  • Pattern Definition: Defines a regex pattern that matches vowels ([aeiou]).
  • Use Matcher: Uses Matcher to find and count all vowel occurrences in the string.
  • Count Vowels: Counts the number of times the regex pattern matches vowels.
  • Output: Displays the total number of vowels in the string.
  • Close Scanner: The scanner is closed to free up system resources.

Java Code Editor:




Follow us on Facebook and Twitter for latest update.