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
tino4ka555 [31]
4 years ago
6

Write a program with functions that accepts a string as an argument and returns the number of vowels that the string contains. T

he application should have another function that accepts a string as an argument and return the number of consonants that the string contains. The application should let the user enter a string and should display the number of vowels and the number of consonants it contains.
Computers and Technology
1 answer:
suter [353]4 years ago
7 0

Answer:

import java.util.Scanner;

public class num11 {

   public static void main(String[] args) {

   Scanner in = new Scanner(System.in);

       System.out.println("Enter a word or phrase");

       String word = in.nextLine();

       //Calling the methods

       System.out.println("Total vowels in "+word+" are "+numVowels(word));

       System.out.println("Total consonants in "+word+" are "+numConsonants(word));

   }

   public static int numVowels(String word){

       //Remove spaces and convert to lowercase

       //Assume that only correct character a-z are entered

       String newWord = word.toLowerCase().replaceAll(" ","");

       int vowelCount = 0;

       for(int i = 0; i <= newWord.length()-1; i++){

           if(newWord.charAt(i)=='a'||newWord.charAt(i)=='e'||newWord.charAt(i)=='i'

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

               vowelCount++;

           }

       }

       return vowelCount;

   }

   public static int numConsonants(String word){

       //Remove spaces and convert to lowercase

       //Assume that only correct character a-z are entered

       String newWord = word.toLowerCase().replaceAll(" ","");

       int consonantCount = 0;

       //Substract total vowels from the length of the word to get the consonants

       consonantCount = newWord.length()-numVowels(word);

       return consonantCount;

   }

}

Explanation:

  • Create two methods vowelCount() and consonantCount() both accepts a string parameter and returns an int
  • vowelCount() Uses a for loop to count the occurrence of the vowels (a,e,i,o,u) in the string and returns the count.
  • consonantCount calls vowelCount and subtracts the vowelCount from the string length
  • In the main Method the user is prompted to enter a string
  • The two methods are called to return the number of vowels and consonants
  • Observe also that it is assumed that user entered only strings containing the character a-Z.
  • Observe also that the string is converted to all lower cases and whitespaces removed.
You might be interested in
Question 4: What will be the output of the code? Show a complete analysis.
Eva8 [605]

Answer:

The output of the code is 8

4 0
4 years ago
Siva added a Contact Form to her website. This is used for the collection to be included in
MAVERICK [17]
This helps people to ask questions or report if something wrong happens but yeah it can be included in the profile collection.hope this helps.
4 0
4 years ago
A difference between crt monitors and flat-panel displays is that most flat-panel displays use digital signals to display images
scoundrel [369]

some crt monitors use analog signals such as rca, coaxial, or bnc connectors.

7 0
4 years ago
In 2007, __________ Floridians died in alcohol-related collisions. A. 501B. 1,051C. 5,015D. 10,839
wel

1000 people died of an alcohol addiction

4 0
4 years ago
A working model of a new product for testing purposes.
kotykmax [81]
Prototype is a working model for a product only for  testing purposes
6 0
3 years ago
Other questions:
  • A. STOP: What is a technology habit you practice today that you now realize will not help you to be successful? Explain why it’s
    10·2 answers
  • A motherboard has four DIMM slots; three slots are gray and the fourth is black. What type of memory is this board designed to u
    6·1 answer
  • Answer the following questions:
    7·1 answer
  • When you reference a cell or range in a different worksheet, the ____ separates the sheet reference from the cell reference.
    10·1 answer
  • When converting the relationships to the relational model and the database schema, it is possible that some of the relationships
    15·1 answer
  • Define ethical (include a link to the source of definition). Describe the ethical dilemma (issue) in this scenario.
    12·1 answer
  • What type of internet connection do you think you'd get in Antarctica?
    14·2 answers
  • Write a python statement that print the number 1000
    6·1 answer
  • Create 2 methods of your choice.
    5·1 answer
  • Magnetic video tape in a plastic casing is a?
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!