Ask Question

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.

+2
Answers (1)
  1. 13 January, 05:36
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers