易百教程

164、编写一个 Java 程序来计算字符串中存在的单词数?

程序实现参考:

public class Test {

    public static void main(String args[]) {
        String s = "Sharma is a good player and he is so punctual";
        String words[] = s.split(" ");
        System.out.println("The Number of words present in the string are : " + words.length);
    }
}

运行结果:

The Number of words present in the string are : 10