Count the number of words in the String

In this article, you will learn how to count the number of words in a string using Java. To count the number of words in a given String in Java, you can use various approaches. One simple and common way is to split the string based on whitespace characters (such as spaces, tabs, or newlines) and then count the number of resulting substrings.

Here are a few different solutions in Java for this:

1. Using whitespace characters

In this approach, we use a boolean variable inWord to track whether we are currently inside a word or not. We iterate through the characters of the input string, and when we encounter a whitespace character, we mark the end of a word (inWord = false). When we encounter a non-whitespace character, we check if it’s the start of a new word (!inWord). If so, we increment the word count and set inWord = true.

This approach doesn’t rely on regular expressions or splitting the string, making it slightly more efficient for large strings with many words.

package com.javacodepoint.string;

public class WordCount {
	
	public static int countWords(String input) {
		if (input == null || input.isEmpty()) {
			return 0; // If the input is null or empty, return 0 words.
		}

		int wordCount = 0;
		boolean inWord = false;

		// Iterate through the characters in the input string
		for (char c : input.toCharArray()) {
			if (Character.isWhitespace(c)) {
				// If a whitespace character is found, mark the end of a word
				inWord = false;
			} else {
				// If a non-whitespace character is found, check if it's the start of a new word
				if (!inWord) {
					wordCount++;
					inWord = true;
				}
			}
		}

		return wordCount;
	}

	public static void main(String[] args) {
		String str = "   Hello,  World! This is a   sample string example.   ";
		int wordCount = countWords(str);

		System.out.println("Input String: " + str);
		System.out.println("Number of words: " + wordCount);
	}
}

OUTPUT:

Input String: Hello, World! This is a sample string example.
Number of words: 8

2. Using the split method

In this program, we use the split method of the String class, which splits the input string into an array of substrings based on the given regular expression pattern. The pattern "\\s+" is used, which represents one or more whitespace characters (spaces, tabs, or newlines).

Before splitting, we use input.trim() to remove any leading or trailing whitespace from the input string. This ensures that leading/trailing spaces do not affect the word count.

Finally, we return the length of the words array, which represents the number of words in the input string.

package com.javacodepoint.string;

public class WordCount2 {

	public static int countWords(String input) {
		if (input == null || input.trim().isEmpty()) {
			return 0; // If the input is null, empty, or contains only whitespace, return 0 words.
		}

		// Split the input string using whitespace as the delimiter
		String[] words = input.trim().split("\\s+");

		// Return the number of words in the array
		return words.length;
	}

	public static void main(String[] args) {
		String str = "This is a sample string example.";
		int wordCount = countWords(str);

		System.out.println("Input String: " + str);
		System.out.println("Number of words: " + wordCount);
	}
}

OUTPUT:

Input String: This is a sample string example.
Number of words: 6

Learn also: Count characters from the string in Java.

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

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