Ask 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 word. The vowels are A, E, I, O, and U (ignore the 'sometimes Y' rule). Count both uppercase and lowercase vowels.

+5
Answers (1)
  1. 9 December, 12:23
    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 & 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