answer.
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]
2 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]2 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
Type the correct answer in the box. Spell all words correctly.
vovikov84 [41]

I'd say the wireless technology.

Sometimes, it can be a hassle to constantly pull the SD card from the camera and plug it into your computer and transfer files. The company Lily is working for can decide to purchase a number of digital cameras with built-in Wi-Fi support that easily facilitates transfer of photos wirelessly. If they have older cameras, a small upgrade is in order: a Wi-Fi SD card.

6 0
3 years ago
Which of the given assertion methods will return true for the code given below? Student student1 = new Student(); Student studen
Svetradugi [14.3K]

Answer:

The true statements are :

A. assertEquals(student1,student2)  

C. assertSame(student1,student3)

Explanation:

AssertEquals() asserts that the objects are equal, but assertSame() asserts that the passed two objects refer to the same object or not, if found same they return true, else return false.

6 0
2 years ago
What will be the output of the following Python code? class A: def test1(self): print(" test of A called ") class B(A): def test
Vlad [161]

Answer:

"test of B called"

Explanation:

  • The object is created for the D class, but there are two test methods in class B and C.
  • The D class does not hold any test method. So when the object called the test method. Then the B method is called.
  • It is because the B class is declared first in the inheritance of D class like in the statement "class D(B, C)".
  • If the statement will be "class D(C, B)", then the C method will be called.
  • if the D class holds the method test, then only the D method will be called.
  • Hence the output is "test of B called".
7 0
2 years ago
A reputable, world-renowned auction house uses blockchain to verify the authenticity of paintings prior to placing them up for s
REY [17]

Answer:

The ledgers are secure, shared, and incorruptible is the correct answer.

6 0
2 years ago
It is an array containing information such as headers, paths and script locations wherein it is created by the web server itself
Naddik [55]

Answer:

c. $_SERVER

Explanation:

$_SERVER is an array in PHP containing information about headers, paths, and script locations.

Example usage:

echo $_SERVER['HTTP_HOST'];

This will print information about HTTP_HOST header.

echo $_SERVER['DOCUMENT_ROOT'];

This provides information about document root path.

echo $_SERVER['SCRIPT_FILENAME'];

This provides information about the currently executing script.

4 0
2 years ago
Other questions:
  • your monthly living expenses are $1500 on an income of $1,650 per month. your goal is to have an emergency fund of 4 times your
    5·1 answer
  • Instructions:
    14·1 answer
  • Initialize the list short_names with strings 'Gus', 'Bob', and 'Zoe'. Sample output for the givenprogram:Gus Bob Zoeshort_names
    13·1 answer
  • Collaboration online increases students' motivation by
    5·2 answers
  • What text results in variable white space (blank) texts results In even white space
    9·1 answer
  • Assume each student is assigned an advisor from a department. Each classroom is assigned a classroom (class# determines Classroo
    14·1 answer
  • Which of the following operation is not performed by a mouse 1) left click , middle click , right click, double click​
    15·2 answers
  • ___________is used for drawing 3D objects in the field of Science and Engineering.
    12·2 answers
  • Suppose that a class named ClassA contains a private nonstatic integer named b, a public nonstatic integer named c, and a public
    14·1 answer
  • Ali has created a small program in Python, but he wants to store his data in a multi-dimensional array. He would like to use adv
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!