1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Umnica [9.8K]
3 years ago
11

consonantCount (s2) Write the function consonantCountthat counts how many times a consonant (either lowercase or uppercase) occu

rs in a string. It takes a string sas input and returnsan integer, the number of consonants in the string. For this question, yand Yare not consonants. Please use a for loop to do the computation. Please do not use a brute force solution (comparing against one consonant at a time): it will not receive credit.
Computers and Technology
1 answer:
Sidana [21]3 years ago
3 0

Answer:

public static int consonantCount(String str){

       int numOfConsonants =0;

       int numOfVowels =0;

       String word = str.toLowerCase();

       for(int i=0; i< word.length(); i++){

           if(word.charAt(i)=='a'||word.charAt(i)=='i'||word.charAt(i)=='o'

                   ||word.charAt(i)=='e'||word.charAt(i)=='u'||word.charAt(i)=='y'){

              numOfVowels++;

           }

       }

       numOfConsonants = (word.length()-numOfVowels);

       return numOfConsonants;

   }

Explanation:

Using Java programming Language

  • Create two variables int numOfConsonants and int numOfVowels To hold the number of consonants and vowels repectively
  • The english vowels for this question includes y or Y
  • Convert the string entered into all lower case letters using java's toLowerCase method
  • Using a for loop count the total number of vowels in the string
  • Subtract the number of vowels in the string from the length of the string to get the number of consonants
  • return the number of consonants
  • Consider a complete program below where a user is prompted to enter a string, the string is stored in a variable, the string is cleaned (Removing all special characters and whitespaces)
  • The method cosonantCount is then called

<em>import java.util.Scanner;</em>

<em>public class num2 {</em>

<em>    public static void main(String[] args) {</em>

<em>        Scanner in = new Scanner(System.in);</em>

<em>        System.out.println("Enter a String");</em>

<em>        String a = in.nextLine();</em>

<em>        String cleanText = a.replaceAll("[^a-zA-Z0-9]", "");</em>

<em>        System.out.println("The number of consonants in the word are "+consonantCount(cleanText));</em>

<em>    }</em>

<em>    public static int consonantCount(String str){</em>

<em>        int numOfConsonants =0;</em>

<em>        int numOfVowels =0;</em>

<em>        String word = str.toLowerCase();</em>

<em>        for(int i=0; i< word.length(); i++){</em>

<em>            if(word.charAt(i)=='a'||word.charAt(i)=='i'||word.charAt(i)=='o'</em>

<em>                    ||word.charAt(i)=='e'||word.charAt(i)=='u'||word.charAt(i)=='y'){</em>

<em>               numOfVowels++;</em>

<em>            }</em>

<em>        }</em>

<em>        numOfConsonants = (word.length()-numOfVowels);</em>

<em>        return numOfConsonants;</em>

<em>    }</em>

<em>}</em>

You might be interested in
Use fuel with the _____________ rating recommended by your vehicle manufacturer. A ) Converter B) Emission C) Exhaust D) Octane
Free_Kalibri [48]
I would say the correct answer is "octane". It is important that you use the fuel with  the correct octane rating recommended by the manufacturer. Among the choices it is only octane that is used as a rating for fuels. Hope this helps.
7 0
3 years ago
Read 2 more answers
I need help pleaseeeeeee!
sertanlavr [38]

Answer:

located on the x axis

the x axis is horizontal and y axis vertical

8 0
3 years ago
Read 2 more answers
Which of the following examples illustrates the Crowding Out Effect?
maks197457 [2]

your answer is c to what your looking for



6 0
3 years ago
If I had a picture of my friend on my camera that I wished to insert into a publication, which icon would I select from the Inse
Juli2301 [7.4K]
The paper clip i think if there is one than click it and it should tell you to pick the photo or download you might have to download it
4 0
3 years ago
PLEASE HELP!!!!!!!!!!!!!!!!!!!!!
Naddik [55]
For step 4, choose the task from the drop-down menu 
I believe your answer would be D. 
I hope this helped.
5 0
3 years ago
Read 2 more answers
Other questions:
  • Nathan, a civil engineer in a construction company, is one of the best performers in the organization. He is assigned challengin
    9·1 answer
  • The read/write heads of a hard disk gently rest on the hard disk platters in order to read and write the data.
    13·1 answer
  • Which variable name is the best to hold the area of a rectangle?<br> 1A<br> area<br> AREA<br> a
    13·1 answer
  • Who is involved in helping shape the emotional impact of a film? (Select all that apply.)
    8·2 answers
  • Pros of mobile devices on young people during their free time
    5·1 answer
  • Pressing the Backspace key deletes the text to the of the insertion point. The left or the right?
    8·2 answers
  • Given an 10-bit two's complement binary number, what is the decimal value of the largest negative integer that can be represente
    10·1 answer
  • 1. Write the syntax of the following HTML commands by using any one attribute
    6·2 answers
  • Write the code to produce a for loop that counts down from 20 to 0 by 2’s and prints the number each time it goes through the lo
    15·1 answer
  • 1.Which thematic group uses technology to direct the behavior of dynamical systems, ensuring that they behave in a predictable m
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!