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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
package com.kkjavatutorials.client; /* * @author KK JavaTutorials * This java program is all about how to find number of Words in a String/input text */ public class CountWordsTest { public static void main(String[] args) { /* Taking input text or String */ String inputText = "Welcome you in KK JavaTutorials"; //String inputText = " Welcome you in KK JavaTutorials "; // String inputText = null; // String inputText =""; // String inputText =" "; int wordsCount1 = countWordsUsingRegularExp(inputText); System.out.println("No. of words:" + wordsCount1); System.out.println("------------------------------------"); int wordsCount2 = countWordsUsingCustomLogic(inputText); System.out.println("No. of words:" + wordsCount2); } //This method does not use direct API to get word count in input string private static int countWordsUsingCustomLogic(String inputText) { // Handle base condition if inputText is null or empty string. if (inputText == null || inputText.trim().isEmpty()) { System.out.println("Enter valid input text/String"); return 0; } int wordsCount = 0; char charArray[] = new char[inputText.length()]; for (int i = 0; i < inputText.length(); i++) { charArray[i] = inputText.charAt(i); // This check is necessary so that we do not try to read beyond the // string length if (i + 1 < inputText.length()) { charArray[i + 1] = inputText.charAt(i + 1); } /* * This logic is that after reading the last character in a word it should be followed by either space or tab, in that case * we increment the count value by 1 */ if ((i + 1 < inputText.length() && (charArray[i] != ' ' && charArray[i] != '\t') && (charArray[i + 1] == ' ' || charArray[i + 1] == '\t')) || (i + 1 == inputText.length() && charArray[i] != ' ')) { wordsCount++; } } return wordsCount; } //This method uses split method of String class to get word count in input string private static int countWordsUsingRegularExp(String inputText) { // Handle base condition if inputText is null or empty string. if (inputText == null || inputText.trim().isEmpty()) { System.out.println("Enter valid input text/String"); return 0; } /* * Here we are using split method of the string class with the regular * expression pattern ("\\s+") of any spaces So it will return a string * array with words */ String[] wordsArray = inputText.trim().split("\\s+"); int countWords = wordsArray.length; return countWords; } } |
If you run this program in your IDE you get blow output::
1 2 3 |
No. of words:5 ------------------------------------ No. of words:5 |
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.