diff --git a/java-strings-3/src/main/java/com/baeldung/string/README.md b/java-strings-3/src/main/java/com/baeldung/string/README.md deleted file mode 100644 index e02980e93f..0000000000 --- a/java-strings-3/src/main/java/com/baeldung/string/README.md +++ /dev/null @@ -1,3 +0,0 @@ -This file exists to ensure this empty directory is committed in Git. - -Please remove this file when this directory is populated. \ No newline at end of file diff --git a/java-strings-3/src/main/java/com/baeldung/string/wordcount/WordCounter.java b/java-strings-3/src/main/java/com/baeldung/string/wordcount/WordCounter.java new file mode 100644 index 0000000000..30275773a6 --- /dev/null +++ b/java-strings-3/src/main/java/com/baeldung/string/wordcount/WordCounter.java @@ -0,0 +1,49 @@ +package com.baeldung.string.wordcount; + +import java.util.StringTokenizer; + +public class WordCounter { + static final int WORD = 0; + static final int SEPARATOR = 1; + + public static int countWordsUsingRegex(String arg) { + if (arg == null) { + return 0; + } + final String[] words = arg.split("[\\pP\\s&&[^']]+"); + return words.length; + } + + public static int countWordsUsingTokenizer(String arg) { + if (arg == null) { + return 0; + } + final StringTokenizer stringTokenizer = new StringTokenizer(arg); + return stringTokenizer.countTokens(); + } + + public static int countWordsManually(String arg) { + if (arg == null) { + return 0; + } + int flag = SEPARATOR; + int count = 0; + int stringLength = arg.length(); + int characterCounter = 0; + + while (characterCounter < stringLength) { + if (isAllowedInWord(arg.charAt(characterCounter)) && flag == SEPARATOR) { + flag = WORD; + count++; + } else if (!isAllowedInWord(arg.charAt(characterCounter))) { + flag = SEPARATOR; + } + characterCounter++; + } + return count; + } + + private static boolean isAllowedInWord(char charAt) { + return charAt == '\'' || Character.isLetter(charAt); + } +} diff --git a/java-strings-3/src/test/java/com/baeldung/string/wordcount/WordCountUnitTest.java b/java-strings-3/src/test/java/com/baeldung/string/wordcount/WordCountUnitTest.java new file mode 100644 index 0000000000..fdd045978f --- /dev/null +++ b/java-strings-3/src/test/java/com/baeldung/string/wordcount/WordCountUnitTest.java @@ -0,0 +1,35 @@ +package com.baeldung.string.wordcount; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import java.util.StringTokenizer; + +public class WordCountUnitTest { + private String string1 = "This is a test sentence with eight words"; + private String string2 = "This#is%a test sentence with eight words"; + + @Test + public void givenStringWith8Words_whenUsingRegexCount_ThenResultEqual8() { + assertEquals(8, WordCounter.countWordsUsingRegex(string2)); + assertEquals(9, WordCounter.countWordsUsingRegex("no&one#should%ever-write-like,this;but:well")); + assertEquals(7, WordCounter.countWordsUsingRegex("the farmer's wife--she was from Albuquerque")); + } + + @Test + public void givenStringWith8Words_whenUsingManualMethod_ThenWordCountEqual8() { + assertEquals(8, WordCounter.countWordsManually(string1)); + assertEquals(9, WordCounter.countWordsManually("no&one#should%ever-write-like,this but well")); + assertEquals(7, WordCounter.countWordsManually("the farmer's wife--she was from Albuquerque")); + } + + @Test + public void givenAStringWith8Words_whenUsingTokenizer_ThenWordCountEqual8() { + assertEquals(8, WordCounter.countWordsUsingTokenizer(string1)); + assertEquals(3, new StringTokenizer("three blind mice").countTokens()); + assertEquals(4, new StringTokenizer("see\thow\tthey\trun").countTokens()); + assertEquals(7, new StringTokenizer("the farmer's wife--she was from Albuquerque", " -").countTokens()); + assertEquals(10, new StringTokenizer("did,you,ever,see,such,a,sight,in,your,life", ",").countTokens()); + } +}