# How to Count Number of Words in String ? Java Coding Exercise Example

The string is very popular among the Interviewer, and you are bound to see some questions on any programming interview, Java Interviews are no exception. Questions based on Java fundamentals like why String is Immutable in Java to questions based on coding skills e.g. reverse String using recursion in Java, String has always troubled candidates. In this article, we will see a similar question, how to count the number of words in Java String. Before jumping to the solution, just read below to make sure what a word means here. It's a sequence of one or more non-space characters.

We will see two examples to find the number of words in Java String, the first one is based upon pure logic, where it goes through all characters from String and then count each word. Second is more interesting than the first one, here we have used a regular expression to find all words.

We split String by white space, passing \\s+ means greedy search i.e. it includes one or more white spaces.  BTW, this is one of the questions I forgot to share when I wrote about Top 20 String coding questions, will include it on that list for sure.

Also, basic knowledge of essential data structure and algorithms is also very important and that's why I suggest all Java programmers join these free Data Structure and Algorithms courses for beginners to improve your knowledge and algorithms skills.

## How to Count Number of Words in Given String?

Write a function in Java that accepts a string argument and returns the number of words in it. A word is a sequence of one or more non-space characters i.e. any character other than '' (empty String). This should be your method signature :

public int wordCount(String word);

This method should return 5 if passed "Java is best programming language" and return 3 if passed "Java is great". Similarly a call to wordCount("    ") should return 0.

### Solution :

In order to implement this method, we need to assume that two words are separated by space. We also need to ignore leading, trailing and multiple spaces between words. One way to solve this problem is  to split String by space and then count a number of parts. We will see two solutions of this problem, here is the first one .

## How do find a number of words String? 2 Examples

In this sample program, we have two methods, first we will count number of words without using regular expression, and second will do the same but using regex.

```/**
* Java Program to count number of words in String
*
* @author  Javin
*/
public class WordCounterProblem{

/*
* This method return word count without using regular expression
*/
public int wordcount(String word) {
if (word == null || word.isEmpty()) {
return 0;
}
int count = 0;
char ch[] = new char[word.length()];
for (int i = 0; i < word.length(); i++) {
ch[i] = word.charAt(i);
if (((i > 0) && (ch[i] != ' ')
&& (ch[i - 1] == ' '))
|| ((ch[0] != ' ')
&& (i == 0))) {
count++;
}
}
return count;
}

/*
* Counting number of words using regular expression.
*/
public int countWord(String word) {
if (word == null) {
return 0;
}
String input = word.trim();
int count = input.isEmpty() ? 0 : input.split("\\s+").length;
return count;
}

}```

## Junit Test Case

Whenever you are asked to write code on Interview, make sure you write some unit test as well. Some time Interviewer will ask you explicitly, but many times they just want to see whether the candidate follows development practices like unit testing and code review.

For this problem, I have written five tests, though in one method for the sake of brevity.

First is a normal space-separated word, second is empty String, third is String with just space and fourth is word separated with multiple white spaces. You can further add unit tests for null String, String without white space, String with leading and trailing whitespace. I leave that to you.

```import static org.junit.Assert.*;
import org.junit.Test;

public class WordCounterTest {

@Test
public void wordCount() {
Testing test = new Testing();
assertEquals(3, test.wordcount("JUnit is Best"));
// string with three words

assertEquals(0, test.wordcount(""));  // empty string
assertEquals(0, test.wordcount("   "));  // no words, just spaces

assertEquals(3, test.wordcount("String   is   Immutable"));
// words with multiple space in between

assertEquals(2, test.wordcount("See you     "));
// words with trailing space
assertEquals(2, test.wordcount("  Good Morning"));
assertEquals(0, test.wordcount(null)); // null check
}

@Test
public void countWord() {
Testing test = new Testing();
assertEquals(3, test.countWord("JUnit is Best"));
assertEquals(0, test.countWord(""));
assertEquals(0, test.countWord("   "));
assertEquals(3, test.countWord("String   is   Immutable"));
assertEquals(2, test.countWord("See you     "));
assertEquals(2, test.countWord("  Good Morning"));
assertEquals(0, test.countWord(null));
}

}```

That's all about how to find a number of words in a Java String. We have seen 2 examples to do that. It's pretty easy using the split() method of the String class, which also accepts regular expressions.

By using regex \\s+ we can split String into words.

As I said, don't forget to write unit tests on interviews, Interviewers always expects you to write production-quality code and unit testing is part of that.

If you like this coding problem and are hungry for more coding questions from Interviews, check out this amazing collection :
• 30 Programming questions from Job Interviews (list)
• 15 Data Structure and Algorithm Questions for Programmers (list)
• Write a Program to solve Producer-Consumer problems in Java. (Solution)
• How to reverse String in Java without using API methods? (Solution)
• Write a Program to check if a number is binary in Java? (Solution)
• How to find the first non-repeated characters from String in Java? (Solution)
• How to remove duplicates from array without using Collection API? (Solution)
• Write a Program to Check if a number is Power of Two or not? (Answer)
• How to Swap Two Numbers without using Temp Variable in Java? (Solution)
• How to check if LinkedList contains loop in Java? (Solution)
• How to find the middle element of LinkedList in one pass? (Solution)
• How to find prime factors of a number in Java? (Solution)
• Write a Program to prevent Deadlock in Java? (Solution)
• How to check if a number is a Palindrome or not? (Solution)
• How to remove duplicates from ArrayList in Java? (Solution)
• Write a Java Program to See if two String are Anagram of each other? (Solution)
• How to count occurrences of  a character in String? (Solution)
• Write a Program to calculate Sum of Digits of a number in Java? (Solution)
• How to check if a number is Prime or not? (Solution)
• Write a Program to find Fibonacci Series of a Given Number? (Solution)
• How to check if a number is an Armstrong number or not? (Solution)
• Write a Program to calculate factorial using recursion in Java? (Solution)
• How to check if Array contains duplicate numbers or not? (Solution)
And now is the quiz time, What is the time complexity of this algorithm for counting number of words in given String? can you improve this by using any data structure or different approach?

Kunal Krishna said...

Why do (i < 0) && (ch[i] != ' ')

when i is assured it will never be negative. i starts from 0 and is never decremented. that's a bogus check to have.

Anonymous said...

Should be

if (((i > 0) ...

Right?

Anonymous said...

Hi, but first method returns 1 always!

Unknown said...

best fit:
String input= "Java is best programming language";
System.out.println(input.split( "\\s" ).length);

Anonymous said...

public static int countWords(String s){
if(s==null || s.isEmpty()){
return 0;
}
int count=0;
boolean iswhitespace=true;
char[] arr =s.toCharArray();
for(int i=0; i<arr.length; i++){
if((iswhitespace==true) && arr[i]!=' '){
count++;
iswhitespace= false;
}else if(arr[i]==' '){
iswhitespace =true;
}
}
return count;
}

Unknown said...

import java.io.*;
class CountWords
{
public static void main (String[] args)
{

System.out.println("total number of words in string");

String s = "happy bday bhai";

int wordCount = 1;

for (int i = 0; i < s.length(); i++)
{
if (s.charAt(i) == ' ')
{
wordCount++;
}
}

System.out.println("Word count is = " + wordCount);
}
}

Anonymous said...

public class WordCount {

public static void main(String[] args) {

findCount(s);

}

private static void findCount(String s) {
// TODO Auto-generated method stub

s=s.replaceAll("\\s+"," ");
String []s1=s.split(" ");
System.out.println(s1.length);

}

}

javin paul said...

@Anonymous, regular expression is indeed best way to count number of words in String. By the way, you don't need to call s=s.replaceAll("\\s+"," ");, you can directly split, words separated by multiple space will also be found by greedy match i..e \\s+

javin paul said...

@k.k, your program will count empty string as word. It is also not considering words separated by multiple spaces.

javin paul said...

@Kunal, and @Annonymous (1st two) that was bug introduced during formatting. It should be (i > 0) && (ch[i] != ' '). That's why you can see that even though JUnit test passed, first method always return 1. Correct it now.

javin paul said...

@Anonymous (May 24), very good solution. good naming convention, I like using whitespace variable, makes it more readable.

Niya said...

Buggy

Anonymous said...

@Niya, what are bugs? I don't see any problem with the code? do you know any particular scenario?

Anonymous said...

split and length!!! all you need

Anonymous said...

Javin Sir,
What if i write "JAVINISBEST" without spaces
and i want to get number of words as 3?

Pramod Nyachyon said...

public class WordCount2 {
public int wordcounot (String word){
if (word==null || word.isEmpty()){
return 0;
}
int count =0;
char ch[] = new char[word.length()];
for(int i = 0; i< word.length();i++){

}
}

}

Unknown said...

class WordCount
{
int wordcount(String s)
{
int x;
String[] s1;
s1=s.split(" ");
int count=0;

for(int i=0;i<s1.length;i++)
{
if(!(s1[i].isEmpty()))
{

count++;
}
}

return count;
}
public static void main(String... a)
{
WordCount wc=new WordCount();
int c=wc.wordcount("ayush sahu");
System.out.println(c);
}

}

Sonu said...

Java code for first non-repeating character in a given string by using toCharArray()