Finding word count for a given string I wrote a program to implement to find the count of words for a given string.
I have applied two different approaches.
The test works for these two approaches and I am looking for other approaches to solve this problem.
Thanks for your valuable comments advance.
package test;

import main.algorithms.WordCount;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class WordCountTest {

    WordCount wc;

    @Before
    public void setUp(){
        wc = new WordCount();
    }

    @Test
    public void testWordCount(){

        String str = &quot;nesly amazon google facebook apple&quot;;

        Assert.assertEquals(5,wc.countWords(str));

        Assert.assertEquals(5,wc.countWordsTokenizer(str));
    }

}


package main.algorithms;

import java.util.StringTokenizer;

public class WordCount {

    public int countWords(String str)
    {
        int end = 0;
        int in = 1;

        int state = end; int wc = 0; int i = 0;

        while (i &lt; str.length())
        {
            if(str.charAt(i) == ' ' || str.charAt(i) == '\n'
                    || str.charAt(i) == '\t')
                state = end;

            else if (state == end)
            {
                state = in;
                ++wc;
            }
            ++i;
        }
        return wc;
    }

    public int countWordsTokenizer(String str){

        return new StringTokenizer(str).countTokens();
    }



}
Your approach works well but it doesn't consider punctuation, for example:
@Test
public void testWordCount(){
    String str = &quot;Hey — how are you?&quot;;

    assertEquals(4, wc.countWords(str)); // Fail
    assertEquals(4, wc.countWordsTokenizer(str)); // Fail
}

Also StringTokenizer is discouraged for new code. From the Javadoc:

StringTokenizer is a legacy class that is retained for compatibility
reasons although its use is discouraged in new code. It is recommended
that anyone seeking this functionality use the split method of
String or the java.util.regex package instead.

The split method accepts a regex as input. This is to split by non-words characters:
public int countWordsSplit(String str) {
    if (str == null || str.isEmpty()) {
        return 0;
    }
    return str.split(&quot;\\W+&quot;).length;
}

Test:
@Test
public void testWordCount(){
    String str = &quot;Hey — how are you?\t\n&quot;;

    assertEquals(4, wc.countWords(str)); // Fail
    assertEquals(4, wc.countWordsTokenizer(str)); // Fail
    assertEquals(4, wc.countWordsSplit(str)); // Pass
}

Edge cases
wc.countWordsSplit(&quot;Bill's house&quot;); // returns 3

wc.countWordsSplit(&quot;1 2&quot;); // returns 2
wc.countWords(&quot;1 2&quot;); // returns 2
wc.countWordsTokenizer(&quot;1 2&quot;); // returns 2

// Letters with accents, umlaut etc.
// Other edge cases...

Considerations
It all depends on how you define what a &quot;word&quot; is in the context of your program.
Define what a word is by making some assumptions. Then find a solution that satisfies your requirements. This approach prevents you from having an over-complicated solution that your program doesn't need.
More info here
Many thanks to @RolandIllig for the helpful comments.