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