The code was very verbose.

Now it is simplified without sacrificing accuracy.
This commit is contained in:
Ayush Varshney
2019-10-03 18:14:08 -04:00
committed by GitHub
parent b8b293855c
commit e3dfdf23ff

View File

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