Ask Question
22 April, 21:08

Write a function called count_vowels that accepts a string argument that represents a word and returns the number of vowels that are in the word. The vowels are A, E, I, O, and U (ignore the 'sometimes Y' rule). Count both uppercase and lowercase vowels.

+1
Answers (1)
  1. 22 April, 23:54
    0
    public static int count_vowels (String str) {

    String word = str. replaceAll ("[^a-zA-Z0-9]", "");

    String text = word. toUpperCase ();

    int lenOfString = text. length ();

    int count = 0;

    for (int i = 0; i
    if (text. charAt (i) = ='A'||text. charAt (i) = ='E'||text. charAt (i) = ='I'

    ||text. charAt (i) = ='O'||text. charAt (i) = ='U') {

    count++;

    }

    }

    return count;

    }

    Explanation:

    This has been implemented using Java programming language.

    Firstly, white spaces, special characters and punctions are removed from the string uisng the replaceAll () method

    The string is converted to all upper cases

    a for loop is used to count the number of vowels and store to a variable count

    In the main method a call to count_vowels is done

    public class num1 {

    public static void main (String[] args) {

    String name = "David %$!. gjak";

    System. out. println ("The number of vowels in "+name + " are "+count_vowels (name));

    }

    public static int count_vowels (String str) {

    String word = str. replaceAll ("[^a-zA-Z0-9]", "");

    String text = word. toUpperCase ();

    int lenOfString = text. length ();

    int count = 0;

    for (int i = 0; i
    if (text. charAt (i) = ='A'||text. charAt (i) = ='E'||text. charAt (i) = ='I'

    ||text. charAt (i) = ='O'||text. charAt (i) = ='U') {

    count++;

    }

    }

    return count;

    }

    }

    The complete program with a Main method is given below
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a function called count_vowels that accepts a string argument that represents a word and returns the number of vowels that are in the ...” 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