Java Program to Count the Number of Vowels in a String

Introduction

Counting the number of vowels in a string is a common task in text processing. In Java, this exercise helps you understand how to manipulate strings and use loops or regular expressions. This guide will walk you through writing a Java program that counts the number of vowels in a given string.

Problem Statement

Create a Java program that:

  • Prompts the user to enter a string.
  • Counts the number of vowels (a, e, i, o, u) in the string, regardless of case.
  • Displays the count of vowels.

Example:

  • Input: "Hello World"
  • Output: Number of vowels: 3

Solution Steps

  1. Read the String: Use the Scanner class to take the string as input from the user.
  2. Initialize a Vowel Counter: Create a variable to store the count of vowels.
  3. Iterate Through the String: Loop through each character in the string and check if it is a vowel.
  4. Count the Vowels: Increment the counter each time a vowel is found.
  5. Display the Vowel Count: Print the total count of vowels.

Java Program

// Java Program to Count the Number of Vowels in a String // Author: https://www.rameshfadatare.com/ import java.util.Scanner; public class VowelCounter { public static void main(String[] args) { // Step 1: Read the string from the user Scanner scanner = new Scanner(System.in); System.out.print("Enter a string: "); String input = scanner.nextLine(); // Step 2: Initialize the vowel counter int vowelCount = 0; // Step 3: Iterate through the string for (int i = 0; i < input.length(); i++) { char ch = input.charAt(i); // Step 4: Check if the character is a vowel if (isVowel(ch)) { vowelCount++; } } // Step 5: Display the vowel count System.out.println("Number of vowels: " + vowelCount); } // Helper method to check if a character is a vowel public static boolean isVowel(char ch) { ch = Character.toLowerCase(ch); return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'; } } 

Explanation

Step 1: Read the String

  • The Scanner class is used to read a string input from the user. The nextLine() method captures the entire line as a string.

Step 2: Initialize the Vowel Counter

  • A simple integer variable vowelCount is initialized to zero. This will hold the count of vowels found in the string.

Step 3: Iterate Through the String

  • A for loop is used to iterate over each character in the string, using the charAt() method to access individual characters.

Step 4: Check if the Character is a Vowel

  • The isVowel() method is a helper function that checks if a given character is a vowel. It converts the character to lowercase to handle case-insensitivity.

Step 5: Display the Vowel Count

  • The program prints the total number of vowels found in the string using System.out.println().

Output Example

Example:

Enter a string: Hello World Number of vowels: 3 

Conclusion

This Java program demonstrates how to count the number of vowels in a user-input string. It covers essential concepts such as string manipulation, loops, and conditionals, making it a valuable exercise for beginners learning Java programming.

Leave a Comment

Scroll to Top