Java program to count number of words in a sentence

In this post, we are going to see how to count a number of words in a sentence in java.

You can simply use String’s split method to split the sentence by space and then find the length of the splitted array.

Let’s write a simple java program to count number of words in a sentence.

When you run above program, you will get below output:

Number of words in sentence: 5
Number of words in sentence: 7

That’s all about Java program to count number of words in a sentence.

Leave a Reply

Your email address will not be published. Required fields are marked *