Write Java program Lab51.java which takes in a string from the user, converts it to an array of characters (char[] word) and calls the method:
which returns the number of vowels in word. (You have to write countVowels(char[]) ).
import java.util.Scanner; public class Lab51 { public static int countVowels(char[] arr) { int count = 0; char ch; for (int i = 0; i < arr.length; i++) { ch = arr[i]; if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { ++count; } } return count; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a string: "); String line = in.nextLine(); char[] arr = line.toCharArray(); System.out.println("Number of vowels is " + countVowels(arr)); } }
Get Answers For Free
Most questions answered within 1 hours.