Skip to content

Commit ad03086

Browse files
authored
Remove main and add tests for CountWords (TheAlgorithms#4210)
1 parent 22002c9 commit ad03086

File tree

2 files changed

+59
-26
lines changed

2 files changed

+59
-26
lines changed

src/main/java/com/thealgorithms/others/CountWords.java

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,34 @@
33
import java.util.Scanner;
44

55
/**
6-
* You enter a string into this program, and it will return how many words were
7-
* in that particular string
8-
*
96
* @author Marcus
107
*/
11-
public class CountWords {
12-
13-
public static void main(String[] args) {
14-
Scanner input = new Scanner(System.in);
15-
System.out.println("Enter your text: ");
16-
String str = input.nextLine();
17-
18-
System.out.println("Your text has " + wordCount(str) + " word(s)");
19-
System.out.println(
20-
"Your text has " + secondaryWordCount(str) + " word(s)"
21-
);
22-
input.close();
8+
final public class CountWords {
9+
private CountWords() {
2310
}
2411

25-
private static int wordCount(String s) {
12+
/**
13+
* @brief counts the number of words in the input string
14+
* @param s the input string
15+
* @return the number of words in the input string
16+
*/
17+
public static int wordCount(String s) {
2618
if (s == null || s.isEmpty()) {
2719
return 0;
2820
}
2921
return s.trim().split("[\\s]+").length;
3022
}
3123

24+
private static String removeSpecialCharacters(String s) {
25+
StringBuilder sb = new StringBuilder();
26+
for (char c : s.toCharArray()) {
27+
if (Character.isLetterOrDigit(c) || Character.isWhitespace(c)) {
28+
sb.append(c);
29+
}
30+
}
31+
return sb.toString();
32+
}
33+
3234
/**
3335
* counts the number of words in a sentence but ignores all potential
3436
* non-alphanumeric characters that do not represent a word. runs in O(n)
@@ -37,17 +39,10 @@ private static int wordCount(String s) {
3739
* @param s String: sentence with word(s)
3840
* @return int: number of words
3941
*/
40-
private static int secondaryWordCount(String s) {
41-
if (s == null || s.isEmpty()) {
42+
public static int secondaryWordCount(String s) {
43+
if (s == null) {
4244
return 0;
4345
}
44-
StringBuilder sb = new StringBuilder();
45-
for (char c : s.toCharArray()) {
46-
if (Character.isLetter(c) || Character.isDigit(c)) {
47-
sb.append(c);
48-
}
49-
}
50-
s = sb.toString();
51-
return s.trim().split("[\\s]+").length;
46+
return wordCount(removeSpecialCharacters(s));
5247
}
5348
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.thealgorithms.others;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.HashMap;
6+
import org.junit.jupiter.api.Test;
7+
8+
9+
class CountWordsTest {
10+
@Test
11+
public void testWordCount() {
12+
HashMap<String, Integer> testCases = new HashMap<>();
13+
testCases.put("", 0);
14+
testCases.put(null, 0);
15+
testCases.put("aaaa bbb cccc", 3);
16+
testCases.put("note extra spaces here", 4);
17+
testCases.put(" a b c d e ", 5);
18+
19+
for (final var tc : testCases.entrySet()) {
20+
assertEquals(CountWords.wordCount(tc.getKey()), tc.getValue());
21+
}
22+
}
23+
24+
@Test
25+
public void testSecondaryWordCount() {
26+
HashMap<String, Integer> testCases = new HashMap<>();
27+
testCases.put("", 0);
28+
testCases.put(null, 0);
29+
testCases.put("aaaa bbb cccc", 3);
30+
testCases.put("this-is-one-word!", 1);
31+
testCases.put("What, about, this? Hmmm----strange", 4);
32+
testCases.put("word1 word-2 word-3- w?o,r.d.@!@#$&*()<>4", 4);
33+
34+
for (final var tc : testCases.entrySet()) {
35+
assertEquals(CountWords.secondaryWordCount(tc.getKey()), tc.getValue());
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)