Ask Question
20 October, 17:07

Write a function that takes in input an English Sentence and prints the total number of vowels and the total number of consonants in the sentence. The function returns nothing. Note the sentence could have special characters like: whitespaces, punctuation marks and numbers.

+4
Answers (1)
  1. 20 October, 17:19
    0
    public static void vowelsConsonantCount (String str) {

    String text = str. toLowerCase ();

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

    int lenWord = word. length ();

    int count = 0;

    for (int i=0; i
    if (word. charAt (i) = ='a'||word. charAt (i) = ='e'||word. charAt (i) = ='i'

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

    count++;

    }

    }

    int consonants = lenWord-count;

    System. out. println ("the total vowels are "+count);

    System. out. println ("The total consonants are: "+consonants);

    }

    Explanation:

    In Java programming language The method uses java's replaceAll () method to remove all spaces and special characters and punctuations. It converts the string entered into all lower cases using toLowerCase () method Calculates the length of the resulting string using the length () method Using if statement the total vowels are counted, subtracting the total vowels from the size of the string gives the total consonants These two values are outputed.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a function that takes in input an English Sentence and prints the total number of vowels and the total number of consonants 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