Count the Number of Vowels in a Given Sentence in Java



Given a sentence (string), let's say str, count the total number of vowels in it. In Java, the String is a non-primitive data type which is used to define sequence of characters. And, the English letters a, e, i, o, and u are known as vowels.

To count the number of vowels in a given sentence, create a variable named count and initialize it with 0. Store the number of occurrences in this variable. Next, compare each character in the sentence with the vowels. If a match occurs increment the count and print it.

Counting Number of Vowels

In Java, we can count number of vowels in a given sentence by ?

  • Using for loop
  • Using switch statement
  • Using regular expressions

Using for loop

Define a for loop to iterate through each character of the given sentence and inside this loop, use an if block to compare and count vowels.

Example

In this example, we are using the for loop to count number of vowels in a given sentence.

public class CountingVowels {
   public static void main(String args[]) {
      int count = 0;
      String sentence = "welcome to tutorialspoint";

      for (int i = 0 ; i < sentence.length(); i++) {
         char ch = sentence.charAt(i);
         if(ch == 'a'|| ch == 'e'|| ch == 'i' ||ch == 'o' ||ch == 'u'|| ch == 'A'|| ch == 'E'|| ch == 'I'|| ch == 'O'|| ch == 'U') {
            count ++;
         }
      }
      System.out.println("Number of vowels in the given sentence: " + count);
   }
}

This code will produce following result ?

Number of vowels in the given sentence: 10

Using switch Statement

In this approach, use the switch statement to compare the vowels with the characters of the given sentence. When a match found increment the count and print the result.

Example

In the following example, we are counting number of vowels in a given sentence using switch statement.

public class CountingVowels {
   public static void main(String args[]) {
      int count = 0;
      String sentence = "How are you today";

      for (int i = 0; i < sentence.length(); i++) {
         char ch = sentence.charAt(i);
         switch (ch) {
            case 'a': case 'e': case 'i': case 'o': case 'u':
            case 'A': case 'E': case 'I': case 'O': case 'U':
               count++;
               break;
          }
      }
      System.out.println("Number of vowels in the given sentence: " + count);
   }
}

On executing, this code will produce the below result ?

Number of vowels in the given sentence: 7

Using Regular Expressions

In Java, a regular expression, sometimes called as regex, is a sequence of characters that defines a search pattern. The java.util.regex package is used to work with regular expressions.

To count the number of vowels, create a Pattern object using vowels, then use Matcher object to find occurrences of this pattern in the input sentence.

Example

The following example illustrate how to count number of vowels in a given sentence using regular expressions.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CountingVowels {
   public static void main(String args[]) {
      int count = 0;
      String sentence = "Hi! Welcome to Tutorialspoint";
      Pattern myPattern = Pattern.compile("[aeiouAEIOU]");
      Matcher match = myPattern.matcher(sentence);
      while (match.find()) {
         count++;
      }
      System.out.println("Number of vowels in the given sentence: " + count);
   }
}

When you run the above code, the following output will be displayed ?

Number of vowels in the given sentence: 11
Updated on: 2024-07-30T16:45:53+05:30

32K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements