Counting Vowels in String Java Program

By | February 19, 2020

Topic: Counting Vowels in String Java, Counting Vowels in User Supplied String Java Program

This topic covers – How to count the number of vowels in a give string using a Java Program.

Source Code:

import java.util.Scanner;
public class CountingVowels {
   public static void main(String args[]){
      int count = 0;
      System.out.println("Enter a sentence :");
      Scanner sc = new Scanner(System.in);
      String sentence = sc.nextLine();
      sentence = sentence.toLowerCase();

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


//Output
//Enter a sentence :
//welcome
//Number of vowels in the given sentence is 3

Leave a Reply

Your email address will not be published. Required fields are marked *