Skip to content

Commit e3dfdf2

Browse files
authored
The code was very verbose.
Now it is simplified without sacrificing accuracy.
1 parent b8b2938 commit e3dfdf2

File tree

1 file changed

+4
-18
lines changed

1 file changed

+4
-18
lines changed

Others/CountChar.java

+4-18
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55

66
/**
7-
* @author Kyler Smith, 2017
7+
* @author blast314
88
* <p>
9-
* Implementation of a character count.
10-
* (Slow, could be improved upon, effectively O(n).
9+
* Counts the number of characters in the text.
1110
*/
1211

1312
public class CountChar {
@@ -24,21 +23,8 @@ public static void main(String[] args) {
2423
* @param str: String to count the characters
2524
* @return int: Number of characters in the passed string
2625
*/
27-
2826
private static int CountCharacters(String str) {
29-
30-
int count = 0;
31-
32-
if (str == "" || str == null) {
33-
return 0;
34-
}
35-
36-
for (int i = 0; i < str.length(); i++) {
37-
if (!Character.isWhitespace(str.charAt(i))) {
38-
count++;
39-
}
40-
}
41-
42-
return count;
27+
str = str.replaceAll("\\s","");
28+
return str.length();
4329
}
4430
}

0 commit comments

Comments
 (0)