Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I count the number of words in a string?

Tags:

java

string

I need to count the number of words and I am assuming the correct way to do it is by calculating the number of times that the previous character in a string is not a letter (ie other characters) because this is to assume that there would be colons,spaces,tabs, and other signs in the string. So at first my idea was to loop through each character and count how many times that you will not get a letter of an alphabet

    for(int i = 0; i < string.length(); i++) {
      for(int j = 0; i < alphabets.length(); j++) {
       if (string.charAt(i-1) == alphabets.charAt(j)) {
           counter++;
       }
     }
   }

However I will always get an array out of bounds because of this. So, I kinda need a little help or another way that can actually be more efficient. I thought of using Matches to only [a-zA-z] but I'm not sure how do I handle a char to be comparable to a string in counting how many times it occurs.

Thank you

like image 400
nfnmy Avatar asked May 20 '12 04:05

nfnmy


2 Answers

You can use String.split() to convert the string into an array, with one word in each element. The number of words is given by the length of the array:

int words = myString.split("\s+").length;
like image 52
Adam Liss Avatar answered Oct 05 '22 13:10

Adam Liss


Your suggestion to use a regex like "[A-Za-z]" would work fine. In a split command, you'd split on the inverse, like:

String[] words = "Example test: one, two, three".split("[^A-Za-z]+");

EDIT: If you're just looking for raw speed, this'll do the job more quickly.

public static int countWords(String str) {
    char[] sentence = str.toCharArray();
    boolean inWord = false;
    int wordCt = 0;
    for (char c : sentence) {
        if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
            if (!inWord) {
                wordCt++;
                inWord = true;
            }
        } else {
            inWord = false;
        }
    }
    return wordCt;
}
like image 23
phatfingers Avatar answered Oct 05 '22 15:10

phatfingers