Java - Write code to give the word count in a given input string.

Requirements

Write code to give the word count in a given input string.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String inputString = "book2s.com";
        System.out.println(wordCountInString(inputString));
    }//from  w  w w. j a  v  a2  s  . c o m

    /***
     * wordCountInString(final String inputString)
     * wordCountInString(final String inputString) gives the word count in a given input string.
     * @param inputString
     * @return count
     */
    public static int wordCountInString(final String inputString) {
        return inputString.split("([\\W\\s]+)").length;
    }
}