Word count in java string

I was wondering how I would write a method to count the number of words in a java string only by using string methods like charAt, length, or substring.

Loops and if statements are okay!

I really appreciate any help I can get! Thanks!

Trufa's user avatar

Trufa

39.6k43 gold badges126 silver badges190 bronze badges

asked May 3, 2011 at 1:26

Philip McQuitty's user avatar

Philip McQuittyPhilip McQuitty

1,0779 gold badges24 silver badges36 bronze badges

0

This would work even with multiple spaces and leading and/or trailing spaces and blank lines:

String trim = s.trim();
if (trim.isEmpty())
    return 0;
return trim.split("\s+").length; // separate string around spaces

More info about split here.

user's user avatar

user

11k6 gold badges23 silver badges83 bronze badges

answered May 3, 2011 at 1:30

Cory G.'s user avatar

4

public static int countWords(String s){

    int wordCount = 0;

    boolean word = false;
    int endOfLine = s.length() - 1;

    for (int i = 0; i < s.length(); i++) {
        // if the char is a letter, word = true.
        if (Character.isLetter(s.charAt(i)) && i != endOfLine) {
            word = true;
            // if char isn't a letter and there have been letters before,
            // counter goes up.
        } else if (!Character.isLetter(s.charAt(i)) && word) {
            wordCount++;
            word = false;
            // last word of String; if it doesn't end with a non letter, it
            // wouldn't count without this.
        } else if (Character.isLetter(s.charAt(i)) && i == endOfLine) {
            wordCount++;
        }
    }
    return wordCount;
}

Michael Yaworski's user avatar

answered May 3, 2011 at 1:32

koool's user avatar

kooolkoool

15k11 gold badges44 silver badges60 bronze badges

2

Hi I just figured out with StringTokenizer like this:

String words = "word word2 word3 word4";
StringTokenizer st = new Tokenizer(words);
st.countTokens();

answered Mar 9, 2015 at 16:27

fredy hernan sanchez montaa Ha's user avatar

2

Simply use ,

str.split("\w+").length ;

answered Feb 9, 2015 at 14:32

Chaulagai's user avatar

ChaulagaiChaulagai

7528 silver badges12 bronze badges

1

public static int countWords(String str){
        if(str == null || str.isEmpty())
            return 0;

        int count = 0;
        for(int e = 0; e < str.length(); e++){
            if(str.charAt(e) != ' '){
                count++;
                while(str.charAt(e) != ' ' && e < str.length()-1){
                    e++;
                }
            }
        }
        return count;
    }

answered Apr 30, 2013 at 18:20

whiteErru's user avatar

whiteErruwhiteErru

2751 gold badge5 silver badges13 bronze badges

 private static int countWordsInSentence(String input) {
    int wordCount = 0;

    if (input.trim().equals("")) {
        return wordCount;
    }
    else {
        wordCount = 1;
    }

    for (int i = 0; i < input.length(); i++) {
        char ch = input.charAt(i);
        String str = new String("" + ch);
        if (i+1 != input.length() && str.equals(" ") && !(""+ input.charAt(i+1)).equals(" ")) {
            wordCount++;
        }
    }

    return wordCount;
 }

answered Aug 23, 2012 at 22:17

Arvind Krishnakumar's user avatar

Use

myString.split("\s+");

This will work.

Paulo Fidalgo's user avatar

Paulo Fidalgo

21.5k7 gold badges101 silver badges113 bronze badges

answered Aug 27, 2012 at 7:15

Deepak's user avatar

DeepakDeepak

3271 silver badge7 bronze badges

There is a Simple Solution You can Try this code

    String s = "hju   vg    jhdgsf  dh gg    g g  g  ";

    String[] words = s.trim().split("\s+");

    System.out.println("count is = "+(words.length));

answered Nov 27, 2015 at 8:55

Abhishek Verma's user avatar

public static int countWords(String input) {
        int wordCount = 0;
        boolean isBlankSet = false;
        input = input.trim();

        for (int j = 0; j < input.length(); j++) {
            if (input.charAt(j) == ' ')
                isBlankSet = true;
            else {
                if (isBlankSet) {
                    wordCount++;
                    isBlankSet = false;
                }
            }

        }

        return wordCount + 1;
    }

answered Feb 5, 2016 at 11:12

Ashish M Jain's user avatar

1

Algo in O(N)

 count : 0;

 if(str[0] == validChar ) :
      count++;
 else :
      for i = 1 ; i < sizeOf(str) ; i++ :

          if(str[i] == validChar AND str[i-1] != validChar)

             count++;

          end if;

      end for;

 end if;

 return count;

answered Nov 7, 2012 at 10:02

UserBSS1's user avatar

UserBSS1UserBSS1

2,0431 gold badge27 silver badges31 bronze badges

    import com.google.common.base.Optional;
    import com.google.common.base.Splitter;
    import com.google.common.collect.HashMultiset;
    import com.google.common.collect.ImmutableSet;
    import com.google.common.collect.Multiset;

    String str="Simple Java Word Count count Count Program";
    Iterable<String> words = Splitter.on(" ").trimResults().split(str);


    //google word counter       
    Multiset<String> wordsMultiset = HashMultiset.create();
    for (String string : words) {   
        wordsMultiset.add(string.toLowerCase());
    }

    Set<String> result = wordsMultiset.elementSet();
    for (String string : result) {
        System.out.println(string+" X "+wordsMultiset.count(string));
    }


add at the pom.xml
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>r09</version>
</dependency>

answered Jun 19, 2013 at 13:29

Ran Adler's user avatar

Ran AdlerRan Adler

3,52128 silver badges27 bronze badges

Counting Words in a String:

This might also help —>

package data.structure.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CountWords {

    public static void main(String[] args) throws IOException {
// Couting number of words in a string
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter Your String");
        String input = br.readLine(); 

        char[] arr = input.toCharArray();
        int i = 0;
    boolean notCounted = true;
    int counter = 0;
    while (i < arr.length) {
        if (arr[i] != ' ') {
            if (notCounted) {
                notCounted = false;
                counter++;
            }
        } else {
            notCounted = true;
        }
        i++;
    }
    System.out.println("words in the string are : " + counter);
}

}

answered Oct 24, 2014 at 20:56

taj.tajinder's user avatar

2

public class TestStringCount {

  public static void main(String[] args) {
    int count=0;
    boolean word= false;
    String str = "how ma ny wo rds are th ere in th is sente nce";
    char[] ch = str.toCharArray();
    for(int i =0;i<ch.length;i++){
        if(!(ch[i]==' ')){
            for(int j=i;j<ch.length;j++,i++){
                if(!(ch[j]==' ')){
                    word= true;
                    if(j==ch.length-1){
                        count++;
                    }
                    continue;
                }
                else{
                    if(word){
                        count++;
                    }
                    word = false;
                }
            }
        }
        else{
            continue;
        }
    }
    System.out.println("there are "+(count)+" words");      
    }
}

Paulo Fidalgo's user avatar

Paulo Fidalgo

21.5k7 gold badges101 silver badges113 bronze badges

answered May 20, 2013 at 17:16

dReAmEr's user avatar

dReAmErdReAmEr

6,8787 gold badges36 silver badges61 bronze badges

import java.util.;
import java.io.
;

public class Main {

public static void main(String[] args) {

    File f=new File("src/MyFrame.java");
    String value=null;
    int i=0;
    int j=0;
    int k=0;
try {
    Scanner  in =new Scanner(f);
    while(in.hasNextLine())
    {
    String a=in.nextLine();
    k++; 
    char chars[]=a.toCharArray();
    i +=chars.length;
    }
    in.close();
    Scanner in2=new Scanner(f);
    while(in2.hasNext())
            {

        String b=in2.next();
        System.out.println(b);
        j++;
            }
   in2.close();

    System.out.println("the number of chars is :"+i);
    System.out.println("the number of words is :"+j);
    System.out.println("the number of lines is :"+k);





}
catch (Exception e) {
    e.printStackTrace();

}


}

}

answered Mar 28, 2015 at 2:15

hassan's user avatar

My idea of that program is that:

package text;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CoutingWords {

    public static void main(String[] args) throws IOException {
        String str;
        int cWords = 1;
        char ch;

        BufferedReader buffor = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Enter text: ");
        str = buffor.readLine();

        for(int i =0; i<str.length(); i++){
            ch = str.charAt(i);
            if(Character.isWhitespace(ch)){ cWords++; }
        }
        System.out.println("There are " + (int)cWords +" words.");
    }
}

answered Feb 5, 2016 at 15:29

Misq's user avatar

I’m new to stackoverflow but I hope my code helps:

private int numOfWordsInLineCounter(String line){

     int words = 0;

         for(int i = 1 ; i<line.length();i++){
         Character ch  = line.charAt(i-1);
         Character bch = line.charAt(i);
             if(Character.isLetterOrDigit(ch) == true && Character.isLetterOrDigit(bch)== false ) words++;
             if(i == line.length()-1 && Character.isLetterOrDigit(bch))words++;
         }
     return words;
 } 

answered Mar 17, 2016 at 12:50

Soorena's user avatar

SoorenaSoorena

4,3125 gold badges29 silver badges41 bronze badges

A string phrase normaly has words separated by space. Well you can split the phrase using the spaces as separating characters and count them as follows.

import java.util.HashMap;

import java.util.Map;

public class WordCountMethod {

    public static void main (String [] args){

        Map<String, Integer>m = new HashMap<String, Integer>();
        String phrase = "hello my name is John I repeat John";
        String [] array = phrase.split(" ");

        for(int i =0; i < array.length; i++){
            String word_i = array[i];
            Integer ci = m.get(word_i);
            if(ci == null){
                m.put(word_i, 1);
            }
            else m.put(word_i, ci+1);
        }

        for(String s : m.keySet()){
            System.out.println(s+" repeats "+m.get(s));
        }
    }

} 

tom's user avatar

tom

21.3k6 gold badges42 silver badges36 bronze badges

answered May 7, 2017 at 11:27

AlketCecaj's user avatar

Taking the chosen answer as a starting point the following deals with a few English language issues including hyphenated words, apostrophes for possessives and shortenings, numbers and also any characters outside of UTF-16:

public static int countWords(final String s) {
    int wordCount = 0;
    boolean word = false;
    final int endOfLine = s.length() - 1;

    for (int i = 0; i < s.length(); i++) {
        // if the char is a letter, word = true.
        if (isWordCharacter(s, i) && i != endOfLine) {
            word = true;
            // if char isn't a letter and there have been letters before,
            // counter goes up.
        } else if (!isWordCharacter(s, i) && word) {
            wordCount++;
            word = false;
            // last word of String; if it doesn't end with a non letter, it
            // wouldn't count without this.
        } else if (isWordCharacter(s, i) && i == endOfLine) {
            wordCount++;
        }
    }
    return wordCount;
}

private static boolean isWordCharacter(final String s, final int i) {
    final char ch = s.charAt(i);
    return Character.isLetterOrDigit(ch)
            || ch == '''
            || Character.getType(ch) == Character.DASH_PUNCTUATION
            || Character.isSurrogate(ch);
}

answered Dec 15, 2017 at 14:35

Dave Bower's user avatar

Dave BowerDave Bower

3,46726 silver badges28 bronze badges

I just put this together. The incrementer in the wordCount() method is a bit inelegant to me, but it works.

import java.util.*;

public class WordCounter {

private String word;
private int numWords;

public int wordCount(String wrd) {
    StringTokenizer token = new StringTokenizer(wrd, " ");
    word = token.nextToken();
    numWords = token.countTokens();
    numWords++;

    return numWords;
}

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    String userWord;

    WordCounter wc = new WordCounter();

    System.out.println("Enter a sentence.");
    userWord = input.nextLine();

    wc.wordCount(userWord);

    System.out.println("You sentence was " + wc.numWords + " words long.");
  }
}

answered Jun 11, 2018 at 16:54

Geoff Zoref's user avatar

create variable count, state. initialize variables
if space is present keep count as it is else increase count.
for eg:

if (string.charAt(i) == ' ' ) {
state = 0;
} else if (state == 0) {
state = 1;
count += 1;

answered Sep 3, 2020 at 16:15

CuriosCoder's user avatar

lambda, in which splitting and storing of the counted words is dispensed with
and only counting is done

String text = "counting w/o apostrophe's problems or consecutive   spaces";

int count = text.codePoints().boxed().collect(
    Collector.of(
        () -> new int[] {0, 0},
        (a, c) -> {
          if( ".,; t".indexOf( c ) >= 0 )
            a[1] = 0;
          else if( a[1]++ == 0 ) a[0]++;
        }, (a, b) -> {a[0] += b[0]; return( a );},
        a -> a[0] ) );

gets: 7

works as a status machine that counts the transitions from spacing characters .,; t to words

answered Dec 10, 2020 at 16:21

Kaplan's user avatar

KaplanKaplan

2,23313 silver badges11 bronze badges

if(str.isEmpty() || str.trim().length() == 0){
   return 0;
}
return (str.trim().split("\s+").length);

answered Mar 9, 2015 at 4:48

Quick and dirty's user avatar

    String a = "Some String";
    int count = 0;
    for (int i = 0; i < a.length(); i++) {

        if (Character.isWhitespace(a.charAt(i))) {
            count++; 
        }
    }
    System.out.println(count+1);

It will count white spaces. However, If we add 1 in count , we can get exact words.

answered Apr 21, 2020 at 12:21

Mahek Shah's user avatar

Today, I am going to share with you Java interview questions from Google, which were asked to one of my readers during the telephonic round. How do you count the number of words in a given String in Java? You can count words in Java String by using the split() method of String. A word is nothing but a non-space character in String, which is separated by one or multiple spaces. By using a regular expression to find spaces and split on them will give you an array of all words in a given String. This was the easy way to solve this problem as shown here, but if you have been asked to write a program to count a number of words in a given String in Java without using any of String utility methods like String.split() or StringTokenizer then it’s a little bit challenging for a beginner programmer.

It’s actually one of the common Java coding questions and I have seen it a couple of times with Java developer interviews of 2 to 4 years of experience, not just with Google but companies like Barclays, Nomura, Citibank, JP Morgan, and Amazon.

The interviewer also put additional constraints like split() is not allowed, you can only use basic methods like charAt(), length(), and substring() along with the loop, operators, and other basic programming tools.

In this article, I’ll share all three ways to solve this problem i.e. first by using String’s split() method and regular expression, second by using StringTokenizer, and third without using any library method like above.

The third one is the most interesting and very difficult to write a complete solution handling all special characters e.g. non-printable ASCII characters. for our purpose, we assume that space character includes tab, space, or newline and anything which is considered as a letter by Character.isLetter() is considered as a word.

Btw, if you are looking for more String based coding problems, you can either check here, or you can check out Data Structures and Algorithms: Deep Dive Using Java course on Udemy.  It not only provides is a collection of common programming questions and solutions from tech giants like Amazon, Google, Facebook, and Microsoft but also useful to refresh your Data Structure and algorithm skills.

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

Without wasting any more of your time, here are my three solutions to this common coding problem. In the first method we will use the split() method of String class, in the second we will use StringTokenizer, and in the third, we’ll solve this problem without using any API or library method.

Solution 1 — Counting words using String.split() method

In this solution, we will use the split() method of java.lang.String class to count the number of words in a given sentence. This solution uses the regular expression «\s+» to split the string on whitespace. The split method returns an array, the length of the array is your number of words in a given String.

 public static int countWordsUsingSplit(String input) {
    if (input == null || input.isEmpty()) {
      return 0;
    }

    String[] words = input.split("\s+");
    return words.length;
  }

If you are new to a regular expression in Java, the s is a character class to detect space including tabs, since needs to be escaped in Java, it becomes \s and because there could be multiple spaces between words we made this regular expression greedy by adding +, hence \s+ will find one more space and split the String accordingly.

You can also see The Complete Java MasterClass course by Tim Buchalaka on Udemy to learn more about the split() method of String class and regular expression in Java. This is also the simplest way to count the number of words in a given sentence.

3 ways to Count words in Java String - Google Interview Questions

Solution 2 — Counting word in String using StringTokenizer

Constructs a string tokenizer for the specified string. The tokenizer uses the default delimiter set, which is » tnrf»: the space character, the tab character, the newline character, the carriage-return character, and the form-feed character. Delimiter characters themselves will not be treated as tokens

public static int countWordsUsingStringTokenizer(String sentence) {
    if (sentence == null || sentence.isEmpty()) {
      return 0;
    }
    StringTokenizer tokens = new StringTokenizer(sentence);
    return tokens.countTokens();
  }

You can see that we have not given any explicit delimiter to StringTokenizer, it uses the default set of delimiters which is enough to find any whitespace and since words are separated by whitespace, the number of tokens is actually equal to the number of words in given String. See Java Fundamentals Part 1 and 2 on Pluralsight for more information on the StringTokenizer class in Java. You need a membership, but even if you don’t have you can use their 10-day free pass to access this course for free.

Solution 3 — Counting word in String without using library method

Here is the code to count a number of words in a given string without using any library or utility method. This is what you may have written in C or C++. It iterates through the String array and checks every character. It assumes that a word starts with a letter and ends with something which is not a letter. Once it encounters a non-letter it increments the counter and starts searching again from the next position.

 public static int count(String word) {
    if (word == null || word.isEmpty()) {
      return 0;
    }

    int wordCount = 0;

    boolean isWord = false;
    int endOfLine = word.length() - 1;
    char[] characters = word.toCharArray();

    for (int i = 0; i < characters.length; i++) {

      // if the char is a letter, word = true.
      if (Character.isLetter(characters[i]) && i != endOfLine) {
        isWord = true;

        // if char isn't a letter and there have been letters before,
        // counter goes up.
      } else if (!Character.isLetter(characters[i]) && isWord) {
        wordCount++;
        isWord = false;

        // last word of String; if it doesn't end with a non letter, it
        // wouldn't count without this.
      } else if (Character.isLetter(characters[i]) && i == endOfLine) {
        wordCount++;
      }
    }

    return wordCount;
  }

This code is complete and we will see the method live in action in the next paragraph where we’ll combine all three solutions in a single Java program and test. 

Java Program to count a number of words in String

Here is our complete Java program to count a number of words in a given String sentence. It demonstrates all three examples we have seen so far like using the String.split() method, using StringTokenizer, and writing your own method to count the number of words without using any third-party library e.g. Google Guava or Apache Commons.

import java.util.StringTokenizer;

/*
 * Java Program to count number of words in String.
 * This program solves the problem in three ways,
 * by using String.split(), StringTokenizer, and
 * without any of them by just writing own logic
 */
public class Main {

  public static void main(String[] args) {

    String[] testdata = { "", null, "One", "O", "Java and C++", "a b c",
        "YouAre,best" };

    for (String input : testdata) {
      System.out.printf(
          "Number of words in stirng '%s' using split() is : %d %n", input,
          countWordsUsingSplit(input));
      System.out.printf(
          "Number of words in stirng '%s' using StringTokenizer is : %d %n",
          input, countWordsUsingStringTokenizer(input));
      System.out.printf("Number of words in stirng '%s' is : %d %n", input,
          count(input));
    }

  }

  /**
   * Count number of words in given String using split() and regular expression
   * 
   * @param input
   * @return number of words
   */
  public static int countWordsUsingSplit(String input) {
    if (input == null || input.isEmpty()) {
      return 0;
    }

    String[] words = input.split("\s+");
    return words.length;
  }

  /**
   * Count number of words in given String using StirngTokenizer
   * 
   * @param sentence
   * @return count of words
   */
  public static int countWordsUsingStringTokenizer(String sentence) {
    if (sentence == null || sentence.isEmpty()) {
      return 0;
    }
    StringTokenizer tokens = new StringTokenizer(sentence);
    return tokens.countTokens();
  }

  /**
   * Count number of words in given String without split() or any other utility
   * method
   * 
   * @param word
   * @return number of words separated by space
   */
  public static int count(String word) {
    if (word == null || word.isEmpty()) {
      return 0;
    }

    int wordCount = 0;

    boolean isWord = false;
    int endOfLine = word.length() - 1;
    char[] characters = word.toCharArray();

    for (int i = 0; i < characters.length; i++) {

      // if the char is a letter, word = true.
      if (Character.isLetter(characters[i]) && i != endOfLine) {
        isWord = true;

        // if char isn't a letter and there have been letters before,
        // counter goes up.
      } else if (!Character.isLetter(characters[i]) && isWord) {
        wordCount++;
        isWord = false;

        // last word of String; if it doesn't end with a non letter, it
        // wouldn't count without this.
      } else if (Character.isLetter(characters[i]) && i == endOfLine) {
        wordCount++;
      }
    }

    return wordCount;
  }

}

Output
Number of words in string '' using split() is : 0 
Number of words in string '' using StringTokenizer is : 0 
Number of words in string '' is : 0 
Number of words in string 'null' using split() is : 0 
Number of words in string 'null' using StringTokenizer is : 0 
Number of words in string 'null' is : 0 
Number of words in string 'One' using split() is : 1 
Number of words in string 'One' using StringTokenizer is : 1 
Number of words in string 'One' is : 1 
Number of words in string 'O' using split() is : 1 
Number of words in string 'O' using StringTokenizer is : 1 
Number of words in string 'O' is : 1 
Number of words in string 'Java and C++' using split() is : 3 
Number of words in string 'Java and C++' using StringTokenizer is : 3 
Number of words in string 'Java and C++' is : 3 
Number of words in string 'a b c' using split() is : 3 
Number of words in string 'a b c' using StringTokenizer is : 3 
Number of words in string 'a b c' is : 3 
Number of words in string 'YouAre,best' using split() is : 1 
Number of words in string 'YouAre,best' using StringTokenizer is : 1 
Number of words in string 'YouAre,best' is : 2 

You can see that our program is working fine and it can correctly identify a number of words in a given String. 

If you want to practice some more of this type of question, you can also check the Cracking the Coding Interview book, one of the biggest collections of Programming Questions, and Solutions from technical interviews. It also includes questions from service-based companies like Infosys, TCS, and Cognizant.

3 ways to count words in Java String

That’s all about how to count a number of words in Java String. I have shown you three ways to solve this problem, first by using the split() method and regular expression, second by using StringTokenizer class, and third without using any library method to solve this problem directly like split or StringTokenizer.

Depending upon your need, you can use any of these methods. The interviewer usually asks you to do it in a third way, so be ready for that. You can also check out the following resources and coding problems to gain more practice and confidence.

Other String based coding problems you may like to solve

  • How to reverse a String in place in Java? (solution)
  • How to find all permutations of a given String in Java? (solution)
  • How to check if a given number is prime or not? (solution)
  • 10 Free Courses to learn Data Structure and Algorithms (courses)
  • How to find the highest occurring word from a text file in Java? (solution)
  • 100+ Data Structure and Algorithms Problems (solved)
  • 10 Books to learn Data Structure and Algorithms (books)
  • How to check if two given Strings are Anagram in Java? (solution)
  • 101 Coding Problems and a few tips for Tech interviews (tips)
  • How to check if the given string is palindrome or not in Java? (solution)
  • How to remove duplicate characters from String in Java? (solution)
  • 10 Data Structure and Algorithms course to crack coding interview (courses)
  • How to check if a String contains duplicate characters in Java? (solution)
  • How to find the highest occurring word from a given file in Java? (solution)
  • How to count vowels and consonants in a given String in Java? (solution)
  • 21 String coding Problems from Technical Interviews (questions)
  • How to reverse words in a given String in Java? (solution)

Thanks for reading this article so far. If you find this String-based Java coding problem from Google and my explanation useful then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

P. S. — If you are preparing for a programming job interview, then you must prepare for an all-important topic like data structure, String, array, etc. One course which can help you with this task is the Grokking the Coding Interview: Patterns for Coding Questions course on Educativa. It contains popular coding interview patterns which will help you to solve most of the problems in your coding interviews.

Count number of words in string Java example shows how to count number of words in string in Java using the charAt method and regular expression.

How to count number of words in Sting in Java?

1) Count the number of words in a string using the charAt method

We can use the charAt method of the String class to count the number of words as given below.

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

package com.javacodeexamples.stringexamples;

public class CountNumberOfWordsExample {

    public static void main(String[] args) {

        String[] sentences = {

            «Count number of word in String»,

            «Hello World»,

            «Java Example. Hello»,

            » String  count  words  «

        };

        for(String sentence : sentences)

            System.out.println(sentence + «: « + countWords(sentence));

    }

    private static int countWords(String str){

        int wordCount = 0;

        //if string is null, word count is 0

        if(str == null)

            return wordCount;

        //remove leading and trailing white spaces first

        str = str.trim();

        //if string contained all the spaces, word count is 0

        if(str.equals(«»))

            return wordCount;

        for(int i = 0; i < str.length(); i++){

            //if character is a space, increase the word count

            if( str.charAt(i) == ‘ ‘ ){

                wordCount++;

            }

        }

        return wordCount;

    }

}

Output

Count number of word in String: 5

Hello World: 1

Java Example. Hello: 2

String  count  words  : 4

Basically we looped through the characters of each of the string values and when we found a space character, we increased the word count by 1. By looking at the output, it seems that we did not take care of the last word since there is no space after the last word in the string. Also, if the string contains more than one consecutive spaces, our code ended up counting it as a separate word hence we got 4 word count in the sentence ” String count words “.

Let’s fix the problem. Have a look at the below given modified code for the countWords method.

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

private static int countWords(String str){

    int wordCount = 0;

    //if string is null, word count is 0

    if(str == null)

        return wordCount;

    //remove leading and trailing white spaces first

    str = str.trim();

    //if string contained all the spaces, word count is 0

    if(str.equals(«»))

        return wordCount;

    for(int i = 0; i < str.length(); i++){

        //if character is a space, increase the word count

        if( str.charAt(i) == ‘ ‘ ){

            wordCount++;

            //skip all the consecutive white spaces

            while(str.charAt(i) == ‘ ‘ && i < str.length() — 1)

                i++;            

        }else{

            /*

             * If we have reached at end of the string,

             * we need to count the last word

             */

            if(i == str.length() 1)

                wordCount++;

        }

    }

    return wordCount;

}

Output

Count number of word in String: 6

Hello World: 2

Java Example. Hello: 3

String  count  words  : 3

We made two changes, first, we added a while loop which skips through all the consecutive white spaces from the string. Second, we added else block which checks whether we have reached at end of the string and if we have, it increments the number of word by one.

Note: This approach does not work for word separators other than space (such as dot, comma or quotes).

2) Using Java regular expression

We can count the number of words using a regular expression pattern “\s+” along with the split method.  The pattern “\s” means space while the “\s+” pattern means one or more spaces in a regular expression.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

private static int countWords(String str){

    int wordCount = 0;

    //if string is null, word count is 0

    if(str == null)

        return wordCount;

    //remove leading and trailing white spaces first

    str = str.trim();

    //if string contained all the spaces, word count is 0

    if(str.equals(«»))

        return wordCount;

    //split the string by one or more space

    String[] temp = str.split(«\s+»);

    //number of array elements is equal to the words

    return temp.length;

}

Output

Count number of word in String: 6

Hello World: 2

Java Example. Hello: 3

String  count  words  : 3

Note: This approach also does not work for word separators other than the space character.

Instead of the “\s+” pattern, the “\w+” pattern should be used. The “\w” pattern matches a word in a regular expression. For example, “Java Example.Hello” string will return word count as 2 using the “\s+” pattern because “Example” and “Hello” are not separated by a space character but the dot. While the “\w+” pattern will return word count as 3 since it matches words as given below.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

private static int countWords(String str){

    int wordCount = 0;

    //if string is null, word count is 0

    if(str == null)

        return wordCount;

    //remove leading and trailing white spaces first

    str = str.trim();

    //if string contained all the spaces, word count is 0

    if(str.equals(«»))

        return wordCount;

    //split the string by one or more space

    String[] temp = str.split(«\w+»);

    //number of array elements is equal to words

    return temp.length;

}

Output

Count number of word in String: 6

Hello World: 2

Java Example.Hello: 3

String  count  words  : 3

This example is a part of the Java String tutorial.

Please let me know your views in the comments section below.

About the author

  • Author
  • Recent Posts

Rahim

I have a master’s degree in computer science and over 18 years of experience designing and developing Java applications. I have worked with many fortune 500 companies as an eCommerce Architect. Follow me on LinkedIn and Facebook.

in Java Programs

March 11, 2023

Write a Java program to count number of words and characters in a text or string, the following java program has been written in multiple ways along with sample outputs as well.

This code is for counting the number of words in a user input string using Java language.

  • The problem here is to count the number of words in a string that is defined by user input.
  • Our constraint here is the inability to get the correct word count in the event of the user input string containing white-spaces at the beginning of the string.
  • The input here is a sentence in the form of a string input by the user.
  • The output here is the count of number of words present in the user-defined input string.

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

import java.util.Scanner;

class  GetWordCount

{

public static void main(String arg[])

{

    int i,count=1;

            Scanner sc=new Scanner(System.in);

    System.out.println(«Enter the string»);

    String name = sc.nextLine();

    for(i=0;i<name.length();i++)

    {

       if(name.charAt(i)==‘ ‘&&name.charAt(i+1)!=‘ ‘)

               {

                 count++;

}

            }    

    System.out.print(«word count of(«+name+«)———->»+count);

}

}

Output:

  • Solution :
  1. The first line in the main method declares int i and int count and initializes the value of count to 1.
  2. Then, a new Scanner class object is initialized and a reference variable sc is set to represent the object. This class is used to take user input in Java. Scanner class is part of the java.util package and hence the import statement in the beginning is stated to import the functionality of the specified class.
  3. The next line prints a statement to the console instructing the user to enter a string.
  4. The .nextLine() method is called by the Scanner object sc. This method reads a line of user input of a string value and stores it to the string variable name.

    for(i=0;i<name.length();i++) {

    if(name.charAt(i)==‘ ‘&&name.charAt(i+1)!=‘ ‘) {

    count++;

         }

    }

  5. This loop iterates over all the characters (including the white-spaces and other special characters which are also considered as characters in Java) in the user-defined string.
  6. For each iteration, a conditional statement checks whether the character in consideration is a whitespace and the next character is not a white-space. It is essentially checking if the given character is a white-space between two words.
  7. The count is incremented by 1 if this condition is satisfied. Keep in mind that the first word is not counted using this technique and hence the value of count is initialized to 1 and not 0.
  8. The print statement in the end of the main method prints out the input and the counted number of words in the input as the output.

Enter the string

hello world welcome to java tutoring

word count of(hello world welcome to java tutoring)>6

Improve Article

Save Article

Like Article

  • Read
  • Discuss(120+)
  • Improve Article

    Save Article

    Like Article

    Given a string, count the number of words in it. The words are separated by the following characters: space (‘ ‘) or new line (‘n’) or tab (‘t’) or a combination of these.

    Method 1: The idea is to maintain two states: IN and OUT. The state OUT indicates that a separator is seen. State IN indicates that a word character is seen. We increment word count when previous state is OUT and next character is a word character. 

    C

    #include <bits/stdc++.h>

    using namespace std;

    #define OUT 0

    #define IN 1

    unsigned countWords(char *str)

    {

        int state = OUT;

        unsigned wc = 0;

        while (*str)

        {

            if (*str == ' ' || *str == 'n' || *str == 't')

                state = OUT;

            else if (state == OUT)

            {

                state = IN;

                ++wc;

            }

            ++str;

        }

        return wc;

    }

    int main(void)

    {

        char str[] = "One two     threen fourtfive ";

        cout<<"No of words : "<<countWords(str);

        return 0;

    }

    C

    #include <stdio.h>

    #define OUT    0

    #define IN    1

    unsigned countWords(char *str)

    {

        int state = OUT;

        unsigned wc = 0; 

        while (*str)

        {

            if (*str == ' ' || *str == 'n' || *str == 't')

                state = OUT;

            else if (state == OUT)

            {

                state = IN;

                ++wc;

            }

            ++str;

        }

        return wc;

    }

    int main(void)

    {

        char str[] = "One two         threen    fourtfive  ";

        printf("No of words : %u", countWords(str));

        return 0;

    }

    Java

    public class GFG {

        static final int OUT = 0;

        static final int IN = 1;

        static int countWords(String str)

        {

            int state = OUT;

            int wc = 0

            int i = 0;

            while (i < str.length())

            {

                if (str.charAt(i) == ' ' || str.charAt(i) == 'n'

                        || str.charAt(i) == 't')

                    state = OUT;

                else if (state == OUT)

                {

                    state = IN;

                    ++wc;

                }

                ++i;

            }

            return wc;

        }

        public static void main(String args[])

        {

            String str = "One two       threen fourtfive  ";

            System.out.println("No of words : " + countWords(str));

        }

    }

    Python3

    OUT = 0

    IN = 1

    def countWords(string):

        state = OUT

        wc = 0

        for i in range(len(string)):

            if (string[i] == ' ' or string[i] == 'n' or

                string[i] == 't'):

                state = OUT

            elif state == OUT:

                state = IN

                wc += 1

        return wc

    string = "One two         threen fourtfive "

    print("No. of words : " + str(countWords(string)))

    C#

    using System;

    class GFG {

        static int OUT = 0;

        static int IN = 1;

        static int countWords(String str)

        {

            int state = OUT;

            int wc = 0;

            int i = 0;

            while (i < str.Length)

            {

                if (str[i] == ' ' || str[i] == 'n'||

                                      str[i] == 't')

                    state = OUT;

                else if (state == OUT)

                {

                    state = IN;

                    ++wc;

                }

                ++i;

            }

            return wc;

        }

        public static void Main()

        {

            String str = "One two     threen fourtfive ";

            Console.WriteLine("No of words : "

                                  + countWords(str));

        }

    }

    PHP

    <?php

    $OUT = 0;

    $IN = 1;

    function countWords($str)

    {

        global $OUT, $IN;

        $state = $OUT;

        $wc = 0;

        $i = 0;

        while ($i < strlen($str))

        {

            if ($str[$i] == " " ||

                $str[$i] == "n" ||

                $str[$i] == "t")

                $state = $OUT;

            else if ($state == $OUT)

            {

                $state = $IN;

                ++$wc;

            }

            ++$i;

        }

        return $wc;

    }

    $str = "One two         threen fourtfive ";

    echo "No of words : " . countWords($str);

    ?>

    Javascript

    <script>

        var OUT = 0;

        var IN = 1;

        function countWords( str)

        {

            var state = OUT;

            var wc = 0;

            var i = 0;

            while (i < str.length)

            {

                if (str[i] == ' ' || str[i] == 'n'||

                                      str[i] == 't')

                    state = OUT;

                else if (state == OUT)

                {

                    state = IN;

                    ++wc;

                }

                ++i;

            }

            return wc;

        }

            var str = "One two     threen fourtfive ";

            document.write("No of words : "

                                  + countWords(str));

    </script>

    Time complexity: O(n)
    Auxiliary Space: O(1)

    This article is compiled by Aarti_Rathi and Narendra Kangralkar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

    Method 2: using String.split() method

    1. Get the string to count the total number of words.
    2. Check if the string is empty or null then return 0.
    3. Use split() method of String class to split the string on whitespaces.
    4. The split() method breaks the given string around matches of the given regular expression and returns an array of string.
    5. The length of the array is the number of words in the given string.
    6. Now, print the result.

    Below is the implementation of the above approach:

    C++

    #include <bits/stdc++.h>

    using namespace std;

    int countWords(string str)

    {

      if (str.size() == 0) {

        return 0;

      }

      vector<string> words;

      string temp = "";

      for (int i = 0; i < str.size(); i++) {

        if (str[i] == ' ') {

          words.push_back(temp);

          temp = "";

        }

        else {

          temp += str[i];

        }

      }

      int count = 1;

      for (int i = 0; i < words.size(); i++) {

        if (words[i].size() != 0)

          count++;

      }

      return count;

    }

    int main()

    {

      string str = "One two       threen fourtfive ";

      cout << "No of words : " << countWords(str);

      return 0;

    }

    Java

    import java.io.*;

    class GFG

    {

        public static int

          countWords(String str)

        {

            if (str == null || str.isEmpty())

                return 0;

            String[] words = str.split("\s+");

            return words.length;

        }

        public static void main(String args[])

        {

            String str =

              "One two       threen fourtfive ";

            System.out.println("No of words : " +

               countWords(str));

        }

    }

    Python3

    def countWords(s):

        if s.strip() == "":

            return 0

        words = s.split()

        return len(words)

    if __name__ == "__main__":

        s = "One two       threen fourtfive "

        print("No of words : ", countWords(s))

    C#

    using System;

    public class GFG

    {

        public static int countWords(String str)

        {

            if (str == null || str.Length == 0)

            {

                return 0;

            }

            String[] words = str.Split(" ");

          int count = 1;

          for(int i=0;i<words.Length;i++){

              if(words[i].Length!=0) count++;

          }

            return count;

        }

        public static void Main(String[] args)

        {

            var str = "One two       threen fourtfive ";

            Console.WriteLine("No of words : " + GFG.countWords(str).ToString());

        }

    }

    Javascript

    function countWords(str)

    {

      if (str.length == 0) {

        return 0;

      }

      words = [];

      var temp = "";

      for (var i = 0; i < str.length; i++) {

        if (str[i] == " ") {

          words.push(temp);

          temp = "";

        }

        else {

          temp += str[i];

        }

      }

      var count = 1;

      for (var i = 0; i < words.length; i++) {

        if (words[i].length != 0)

          count++;

      }

      return count;

    }

      var str = "One two       threen fourtfive ";

      console.log("No of words : " +countWords(str));

    Time Complexity: O(N)
    Auxiliary Space: O(1)

    Method 3:  using StringTokenizer.countTokens() method

    1. Get the string to count the total number of words.
    2. Check if the string is empty or null then return 0.
    3. Create a StringTokenizer with the given string passed as a parameter.
    4. Count the total number of words in the given string using the countTokens() method.
    5. Now, print the result.

    Below is the implementation of the above approach:

    C++

    #include <bits/stdc++.h>

    using namespace std;

    int countWords(string s)

    {

        if (s.empty())

            return 0;

        istringstream is(s);

        int count = 0;

        string line;

        while (getline(is, line, '/'))

            ++count;

        return count;

    }

    int main()

    {

        string str = "One/ two /      three/n four/tfive ";

        cout << "No of words: " << countWords(str) << endl;

    }

    Java

    import java.util.StringTokenizer;

    class GFG

    {

        public static int

          countWords(String str)

        {

            if (str    == null || str.isEmpty())

                return 0;

            StringTokenizer tokens = new

              StringTokenizer(str);

            return tokens.countTokens();

        }

        public static void main(String args[])

        {

            String str =

              "One two       threen fourtfive ";

            System.out.println("No of words: " +

              countWords(str));

        }

    }

    Python3

    def count_words(s):

        if not s:

            return 0

        count = 0

        lines = s.split("/")

        for line in lines:

            if line.strip():

                count += 1

        return count

    s = "One/ two /      three/n four/tfive "

    print("No of words:", count_words(s))

    C#

    using System;

    class GFG

    {

      public static int

        countWords(String str)

      {

        if (string.IsNullOrEmpty(str))

          return 0;

        string[] tokens = str.Split(' ');

        return tokens.Length;

      }

      public static void Main()

      {

        string str =

          "One two     threen fourtfive ";

        Console.Write("No of words: " +

                      countWords(str));

      }

    }

    Javascript

    function countWords(s)

    {

      if (s.length === 0) return 0;

      const lines = s.split("/");

      return lines.length;

    }

    const str = "One/ two /      three/n four/tfive ";

    console.log(`No of words: ${countWords(str)}`);

    Time Complexity: O(N)
    Auxiliary Space : O(1)

    Method 4: using Character.isLetter() method

    1. Get the string to count the total number of words.
    2. Check if the string is empty or null then return 0.
    3. Converting the given string into a character array.
    4. Check if the character is a letter and index of the character array doesn’t equal to the end of the line that means, it is a word and set isWord by true.
    5. Check if the character is not a letter that means there is a space, then we increment the wordCount by one and set the isWord by false.
    6. Check for the last word of the sentence and increment the wordCount by one.
    7. Now, print the result.

    Below is the implementation of the above approach:

    C++

    #include <bits/stdc++.h>

    using namespace std;

    int countWords(char* str, int n)

    {

        if (n == 0) {

            return 0;

        }

        int wordCount = 0;

        bool isWord = false;

        int endOfLine = n - 1;

        for (int i = 0; i < n; i++) {

            if (isalpha(str[i]) && i != endOfLine) {

                isWord = true;

            }

            else if (!isalpha(str[i]) && isWord)

            {

                wordCount++;

                isWord = false;

            }

            else if (isalpha(str[i]) && i == endOfLine) {

                wordCount++;

            }

        }

        return wordCount;

    }

    int main()

    {

        char str[] = "One two     threen fourtfive ";

        int n = (sizeof(str) / sizeof(char)) - 1;

        cout << "No of words : " << countWords(str, n);

        return 0;

    }

    Java

    import java.io.*;

    class GFG

    {

        public static int

          countWords(String str)

        {

            if(str    == null || str.isEmpty())

                return 0;

            int wordCount = 0;

            boolean isWord = false;

            int endOfLine = str.length() - 1;

            char[] ch = str.toCharArray();

            for (int i = 0; i < ch.length; i++) {

                if (Character.isLetter(ch[i])

                    && i != endOfLine)

                    isWord = true;

                else if (!Character.isLetter(ch[i])

                         && isWord) {

                    wordCount++;

                    isWord = false;

                }

                else if (Character.isLetter(ch[i])

                         && i == endOfLine)

                    wordCount++;

            }

            return wordCount;

        }

        public static void main(String args[])

        {

            String str =

              "One two       threen fourtfive ";

            System.out.println("No of words : " +

              countWords(str));

        }

    }

    Python3

    def countWords(Str):

        if(Str == None or len(Str) == 0):

            return 0

        wordCount = 0

        isWord = False

        endOfLine = len(Str) - 1

        ch = list(Str)

        for i in range(len(ch)):

            if(ch[i].isalpha() and i != endOfLine):

                isWord = True

            elif(not ch[i].isalpha() and isWord):

                wordCount += 1

                isWord = False

            elif(ch[i].isalpha() and i == endOfLine):

                wordCount += 1

        return wordCount

    Str =  "One two       threen fourtfive "

    print("No of words :", countWords(Str))

    C#

    using System;

    public class GFG

    {

      static int countWords(String str)

      {

        if(str == null)

        {

          return 0;

        }

        int wordCount = 0;

        bool isWord = false;

        int endOfLine = str.Length - 1;

        char[] ch = str.ToCharArray();

        for (int i = 0; i < ch.Length; i++)

        {

          if (Char.IsLetter(ch[i]) 

              && i != endOfLine)

          {

            isWord = true;

          }

          else if (!Char.IsLetter(ch[i]) 

                   && isWord)

          {

            wordCount++;

            isWord = false;

          }

          else if (Char.IsLetter(ch[i])

                   && i == endOfLine)

          {

            wordCount++;

          }

        }

        return wordCount;

      }

      static public void Main ()

      {

        string str = "One two       threen fourtfive ";

        Console.WriteLine("No of words : " + countWords(str));

      }

    }

    Javascript

    <script>

    function countWords(str)

    {

            if(str    == null || str.length==0)

                return 0;

            let wordCount = 0;

            let isWord = false;

            let endOfLine = str.length - 1;

            let ch = str.split("");

            for (let i = 0; i < ch.length; i++) {

                if (isLetter(ch[i])

                    && i != endOfLine)

                    isWord = true;

                else if (!isLetter(ch[i])

                         && isWord) {

                    wordCount++;

                    isWord = false;

                }

                else if (isLetter(ch[i])

                         && i == endOfLine)

                    wordCount++;

            }

            return wordCount;

    }

    function isLetter(c) {

      return c.toLowerCase() != c.toUpperCase();

    }

    let str="One two       threen fourtfive ";

    document.write("No of words : " +

              countWords(str));

    </script>

    Time Complexity: O(N)
    Auxiliary Space: O(1) 

    Like Article

    Save Article

    Like this post? Please share to your friends:
  • Word count in google docs
  • Word count in english language
  • Word count in chinese
  • Word count in a dictionary
  • Word count function in word