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.

Respuesta :

ijeggs

Answer:

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<lenOfString; 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<lenOfString; 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

The function called count_vowels that accepts a string argument that represents a word and returns the number of vowels that are in the word is as follows:

def count_vowel(text):

    vowel = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']

    count = 0

    for i in text:

         if i in vowel:

              count += 1

    return count

print(count_vowel("AEBcinjyu"))

Code explanation

The code is written in python.

  • We define a function named count_vowel and it takes a parameter called "text".
  • We stored all the vowels in a variable called vowel.
  • Then, we initialise count to zero.
  • We loop through the text and check if any of the users input has a vowel in it.
  • If the users input has any vowel in it, it will add 1 to count.
  • Then we return count.
  • Finally, we call our function with the required parameter.

learn more on python here:https://brainly.com/question/26104476

Ver imagen vintechnology