Here you can find the source of countWords(String str)
static int countWords(String str)
//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; } }