From e3dfdf23ff9d9980eeccc68ce8f31d0a61768b41 Mon Sep 17 00:00:00 2001 From: Ayush Varshney <34279863+blast314@users.noreply.github.com> Date: Thu, 3 Oct 2019 18:14:08 -0400 Subject: [PATCH] The code was very verbose. Now it is simplified without sacrificing accuracy. --- Others/CountChar.java | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/Others/CountChar.java b/Others/CountChar.java index 217a3be238f8..ee83bce06a51 100644 --- a/Others/CountChar.java +++ b/Others/CountChar.java @@ -4,10 +4,9 @@ /** - * @author Kyler Smith, 2017 + * @author blast314 *

- * 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 static void main(String[] args) { * @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(); } }