Java Word Count countWords(String segment, char[] wordDelimiters)

Here you can find the source of countWords(String segment, char[] wordDelimiters)

Description

Count Words

License

Creative Commons License

Parameter

Parameter Description
segment a parameter
wordDelimiters a parameter

Declaration

public static int countWords(String segment, char[] wordDelimiters) 

Method Source Code

//package com.java2s;
//License from project: Creative Commons License 

public class Main {
    /**/*from w ww. ja  v a2s. co m*/
     * Count Words
     * 
     * @param segment
     * @param wordDelimiters
     * @return
     */
    public static int countWords(String segment, char[] wordDelimiters) {
        int wordCount = 0;
        boolean lastWasGap = true;

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

            boolean isDelimiter = false;
            for (int j = 0, isize = wordDelimiters.length; j < isize; j++) {
                if (ch == wordDelimiters[j]) {
                    isDelimiter = true;
                    lastWasGap = true;
                }
            }

            if (!isDelimiter && lastWasGap) {
                lastWasGap = false;
                wordCount++;
            }
        }
        return wordCount;
    }
}

Related

  1. countWords(String str)
  2. countWords(String str)
  3. countWords(String string)
  4. countWords(String string)