Skip to content

Commit 98c94e1

Browse files
authored
Merge pull request #954 from blast314/patch-3
The code was very verbose.
2 parents b8b2938 + e3dfdf2 commit 98c94e1

File tree

1 file changed

+4
-18
lines changed

1 file changed

+4
-18
lines changed

Others/CountChar.java

Lines changed: 4 additions & 18 deletions
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)