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
Svetllana [295]
4 years ago
11

In Java write a method called wordCount that accepts a String as its parameter and returns the number of words in the String. A

word is a sequence of one or more nonspace characters (any character other than ' '). For example, the call wordCount("hello") should return 1, the call wordCount("how are you?") should return 3, the call wordCount(" this string has wide spaces ") should return 5, and the call wordCount(" ") should return 0.
Computers and Technology
1 answer:
mamaluj [8]4 years ago
5 0

Answer:

// here is code in java.

import java.util.*;

// class definition

class Main

{

   // method that return the number of words in the string

   public static int wordCount(String st)

   {

       /* split the string by space " " delimiter

       and save them into  string array*/

       String[] no_words=st.split(" ");

       // find the length of array

       int len= no_words.length;

       //return the length

       return len;

   }

   // driver function

public static void main (String[] args) throws java.lang.Exception

{

   try{

    // scanner object to read input string

       Scanner s=new Scanner(System.in);

        // string variable

       String str;

       //ask user to enter string

       System.out.print("please enter the string: ");

       // read the string from user

       str=s.nextLine();

       // call the function with string parameter

      int tot_words= wordCount(str);

      // print the number of word in string

      System.out.println("total words in the string is : "+tot_words);

   }catch(Exception ex){

       return;}

}

}

Explanation:

Read a string from user with the help of scanner class object.Call the function

wordCount() with string parameter.Here split the string by space " " delimiter.

Save the words into a string array.Find the length of the array and return it.

Then in the main print the number of words in the input string.

Output:

please enter the string: this string has wide spaces

total words in the string is : 5

You might be interested in
Lines, block arrows, stars, and banners are examples of which of the following
bija089 [108]

Answer:

Lines, block arrows, stars and banners are examples of which of the following is smart art graphic

7 0
3 years ago
A poker hand consists of 5 cards drawn at random without replacement from a 52 card deck. Using python and the cards data frame
PSYCHO15rus [73]

Answer:

# Python program to shuffle a deck of card

# importing modules

import itertools, random

# make a deck of cards

deck = list(itertools.product(range(1,14),['Spade','Heart','Diamond','Club']))

# shuffle the cards

random.shuffle(deck)

# draw five cards

print("You got:")

for i in range(5):

  print(deck[i][0], "of", deck[i][1])

Output

You got:

5 of Heart

1 of Heart

8 of Spade

12 of Spade

4 of Spade

Explanation:

5 0
3 years ago
408 request timeout your browser didn't send a complete request in time
Alik [6]
<span>408 Request Time out error messages are often customized by every website. These messages are especially for large websites that take long time to process itself.
</span>
<span /><span>Causes for these messages:
The 408 Request Timeout error is an HTTP status code that says that your request is not send to the server in the given time. In other words, your website connection is “timed out”
</span>


6 0
4 years ago
A golf ball is at rest on the grass. What must happen to cause the ball to move?
kondaur [170]
It would be friction
Hitting the ball will create friction!
8 0
3 years ago
Select the correct answer.
snow_tiger [21]

Answer: I think it's D.

Explanation: I'm sorry if I chose the wrong answer, I'm not too good with stuff like this.

3 0
2 years ago
Other questions:
  • An enterprise DBMS is automatically capable of serving as a mobile DBMS. There are no special issues raised by mobility. True Fa
    11·1 answer
  • Write a function named intro that takes a string as input.
    12·1 answer
  • What is the subjects under artificial intelligence ?​
    15·1 answer
  •   Why does a shaded-pole motor run at a constant speed?  A. The lines of force do not change direction.  B. The current through
    15·1 answer
  • 17. When an industrial electrical circuit requires a 220 VAC single phase power supply, the electric power supply circuit is nor
    15·1 answer
  • While in class you were introduced to the varieties
    7·1 answer
  • Identify the final stage of the object found in the image in the given text
    9·1 answer
  • What are the common camera hazards? When might you encounter these hazards in your life, and what do you plan to
    14·1 answer
  • Project planning output is :
    9·1 answer
  • A mobile phone app asks the user to point the phone's camera at a room and then applies the phone's lidar scanning function. wha
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!