Ask Question
15 August, 01:47

Write a program named CountVowelsModularized that passes a string to a method that returns the number of vowels in the string.

Note: For testing purposes, Y will not be counted as a vowel.

+5
Answers (1)
  1. 15 August, 02:12
    0
    import java. util. Scanner;

    public class CountVowelsModularized {

    public static void main (String[] args) {

    Scanner in = new Scanner (System. in);

    //Prompt user to enter a string, receive it and assign to a variable

    System. out. println ("Enter a string Value");

    String word = in. nextLine ();

    //Calling the method numVowels and passing the string

    System. out. println ("The Number of vowels are " + numVowels (word. toLowerCase ()));

    }

    }

    The method definition and complete program is given in the explanation section

    Explanation:

    import java. util. Scanner;

    public class CountVowelsModularized {

    public static void main (String[] args) {

    Scanner in = new Scanner (System. in);

    System. out. println ("Enter a string Value");

    String word = in. nextLine ();

    System. out. println ("The Number of vowels are " + numVowels (word. toLowerCase ()));

    }

    public static int numVowels (String string) {

    int counter = 0;

    for (int i = 0; i < string. length (); i++) {

    if (string. charAt (i) = = 'a' || string. charAt (i) = = 'e' || string. charAt (i) = = 'i'

    || string. charAt (i) = = 'o' || string. charAt (i) = = 'u') {

    counter++;

    }

    }

    return counter;

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a program named CountVowelsModularized that passes a string to a method that returns the number of vowels in the string. Note: For ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers