Count number of words in a string in java

By | July 21, 2018

In this post of Count number of words in a string in java We will learn how to count number of words in a string in java.

Write a java program to count the number of words in a string, often asked in the Java interview. To test the logical thinking of the candidates, it is often asked to write this program without using any string function.

Here We are going to learn,how  to count number of words in a String in Java in two different ways.The first approach  using the String class split() method and second approach using char array and String (this logic doesn’t use any of the inbuilt String class method) 

You can refer how to split a string by space in java for better clarity.

If you run this program in your IDE you get blow output::

Watch it on YouTube
Count number of words in a string in java

That’s all about How to Count number of words in a string in java.
If you have any feedback or suggestion please feel free to drop in blow comment box.

Leave a Reply

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