From c29394e969ec5379e0cb4d6b6856525fb1a369c6 Mon Sep 17 00:00:00 2001 From: Raghav Gupta Date: Sun, 3 Oct 2021 01:22:51 +0530 Subject: [PATCH] Create WordCounter.java Java program to count no of words from given input string. --- WordCounter.java | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 WordCounter.java diff --git a/WordCounter.java b/WordCounter.java new file mode 100644 index 0000000..3cdc486 --- /dev/null +++ b/WordCounter.java @@ -0,0 +1,44 @@ +public class demo { + + static final int OUT = 0; + static final int IN = 1; + + // returns number of words in str + static int countWords(String str) + { + int state = OUT; + int wc = 0; // word count + int i = 0; + + // Scan all characters one by one + while (i < str.length()) + { + // If next character is a separator, set the + // state as OUT + if (str.charAt(i) == ' ' || str.charAt(i) == '\n' + || str.charAt(i) == '\t') + state = OUT; + + + // If next character is not a word separator + // and state is OUT, then set the state as IN + // and increment word count + else if (state == OUT) + { + state = IN; + ++wc; + } + + // Move to next character + ++i; + } + return wc; + } + + // Driver program to test above functions + public static void main(String args[]) + { + String str = "One two three\n four\tfive "; + System.out.println("No of words : " + countWords(str)); + } +}