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.

You May Also Like:

Reverse a string in Java (6 Different Ways)
Splitting a String in Java using a delimiter
Count total number of times each character appears in the string in java
Check if two strings are anagrams or not in java
How to convert string to int without using library functions in java
Check Whether a Given String/Number is a Palindrome in java
How to find first non-repeated character in a given String in Java
How to find first non-repeatable character from a String using Java 8
Java Program to find the frequency of each character in String ?

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 *