Merge pull request #954 from blast314/patch-3

The code was very verbose.
This commit is contained in:
Yang Libin 2019-10-04 16:42:40 +08:00 committed by GitHub
commit 98c94e17eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,10 +4,9 @@ import java.util.Scanner;
/**
* @author Kyler Smith, 2017
* @author blast314
* <p>
* Implementation of a character count.
* (Slow, could be improved upon, effectively O(n).
* Counts the number of characters in the text.
*/
public class CountChar {
@ -24,21 +23,8 @@ public class CountChar {
* @param str: String to count the characters
* @return int: Number of characters in the passed string
*/
private static int CountCharacters(String str) {
int count = 0;
if (str == "" || str == null) {
return 0;
}
for (int i = 0; i < str.length(); i++) {
if (!Character.isWhitespace(str.charAt(i))) {
count++;
}
}
return count;
str = str.replaceAll("\\s","");
return str.length();
}
}