From eae610b700d47dcac592c88b2f0b6928bfd6f3d5 Mon Sep 17 00:00:00 2001 From: st <31894520+ShagiG@users.noreply.github.com> Date: Mon, 30 Oct 2017 11:45:06 +0530 Subject: [PATCH] number-of-words-in-a-string The program can count the number of words in the user String input and print them --- number-of-words-in-a-String | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 number-of-words-in-a-String diff --git a/number-of-words-in-a-String b/number-of-words-in-a-String new file mode 100644 index 0000000..7bb0ea9 --- /dev/null +++ b/number-of-words-in-a-String @@ -0,0 +1,22 @@ + + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class WordCount { + + public static void main(String[] args) throws IOException { + System.out.print("Please enter a string: "); + BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + String string = reader.readLine(); + + String[] words = string.split("\\s+"); // match one or more spaces + + System.out.println("\""+string+"\""+" has "+words.length+" words"); + System.out.println("The words are, "); + for(int i =0;i