Write a program that calls a method that takes a character array as a parameter (you can initialize an array in the main method) and returns the number of vowels in the array. Partial program is given below.
Program code:
//define class CountVowels
public class CountVowels
{
//define a method to count the vowels
static int vowels(char arr[])
{
//declare the variables
int count = 0;
char ch;
//iterate a for loop
for (int i = 0; i < arr.length; i++)
{
//get the first charecter to ch
ch = arr[i];
//check if ch is a vowel
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
//increment the count by 1
count++;
}
//return the value of count
return count;
}
//define main function
public static void main(String[] args)
{
//charecter array
char[] arr={'H','e','l','l','o'};
//print the number of vowels
System.out.print("There are "+vowels(arr)+" vowels in the array");
}
}
Program code #1:
Step by step
Solved in 3 steps with 2 images