Java Word Count countWords(String str)

Here you can find the source of countWords(String str)

Description

count Words

License

Open Source License

Declaration

static int countWords(String str) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    static int countWords(String str) {
        if (str.equals("") || str == null) {
            return 0;
        }/*w w w.j  ava2s  . com*/
        int numberSpaces = 0;
        for (char c : str.toCharArray()) {
            if (c == ' ') {
                numberSpaces++;
            }
        }

        return ++numberSpaces;
    }
}

Related

  1. countWords(String segment, char[] wordDelimiters)
  2. countWords(String str)
  3. countWords(String string)
  4. countWords(String string)