Merge pull request #7579 from Alfred-Samanga/master

BAEL-3120 alfred.samanga@gmail.com
This commit is contained in:
rpvilao 2019-08-24 10:41:09 +02:00 committed by GitHub
commit 4c6d95a261
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 3 deletions

View File

@ -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.

View File

@ -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);
}
}

View File

@ -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());
}
}