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));
    }/*  w ww . j  a v a 2 s .co  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;
    }
}

Related Exercise