In: Computer Science
Create a Java method that takes a String as input value and returns the number of vowels contained in that string.
CODE:
import java.util.Scanner;
public class CountVowelsInString {
   //method to count number of vowels in a string
   public static int CountNumberOfVowels(String
str){
       int vowelCount = 0; // the variable
that holds the vowelCount
       // to loop through the
string
       for (int i = 0; i <
str.length(); i++) {
           char ch =
str.toLowerCase().charAt(i);
           // if the
character at the position is equal to any of the vowel
           if((ch=='a') ||
(ch=='e') || (ch=='i') || (ch=='o') || (ch=='u') ){
          
    vowelCount++; //increment the vowelCount
           }
       }
       //return statement
       return vowelCount;
   }
   public static void main(String[] args) {
      
       Scanner scan = new
Scanner(System.in);// instantiating the Scanner class
       System.out.println("Please enter a
string...");// prompt to enter a string
       String str = scan.nextLine();//
getting the String input and storing into str
       int count =
CountNumberOfVowels(str); // calling the method and getting the
number of vowels
       System.out.println(str + " has "
+ count + " number of vowels"); //writing the number of
vowels
   }
}
OUTPUT:
