Topics
1. there is a string, in any language, the number of occurrences of all the letters in the string, not case-sensitive
(all letters are counted in uppercase, if there are lowercase letters need to be counted in the corresponding capital count) in
addition, write the test case data to debug the program, Includes input and expected output.
as input: abca5z Expected output: A 2, B 1, C 1, Z 1
input: " $3@ expected output: no letter Reason: Consider a case that does not include letters
:
1. Key functions and Code Snippets add note
2. Hint: ASCII code of lowercase letter –32 = Uppercase ASCII code
3. Test case data must not be less than 10, and please write down the reason for using this data
Code
public class CharCount {public void count (String str) {if (str = = NULL | | str.length () = = 0) {return;
} str = Str.touppercase ();
int length = Str.length ();
Pattern pattern = Pattern.compile ("[a-z]+");
Matcher Matcher = Pattern.matcher (str);
StringBuilder builder = new StringBuilder (length);
int k = 0;
while (K < length) {if (Matcher.find ()) {Builder.append (Matcher.group ());
if (matcher.end () = = length) {//System.out.println ("over");
Break
}} k++;
} if (builder.length () = = 0) {System.out.println ("no characters");
Return
} map<character, integer> countmap = new Linkedhashmap<character, integer> ();
for (int i = 0; i < builder.length (); i++) {Character c = builder.charat (i);
if (Countmap.containskey (c)) {int num = Countmap.get (c);
Countmap.put (c, ++num);
} else {countmap.put (c, 1);
}} System.out.println ("Contains characters:" +builder);
Printcountinfo (COUNTMAP); } private voidPrintcountinfo (Map<character, integer> Map) {if (!map.isempty ()) {set<entry<character, Integer>>
Set = Map.entryset ();
Iterator<entry<character, integer>> iter = Set.iterator ();
while (Iter.hasnext ()) {entry<character, integer> Entry = Iter.next ();
System.out.println (String.Format ("%c:%d", Entry.getkey (), Entry.getvalue ())); }}} public static void Main (string[] args) {//TODO auto-generated method stub string[] Strarray = {"123456", Test no characters, can distinguish the number "" $@!-= (&^%#@! ",//test without characters, can distinguish the special symbol" Asdfsewrewe ",//test lowercase character statistics" ASDFDSFWEGSDFSD ",//test Statistics for uppercase characters "12324aasdfds",//test case for lowercase characters and non-character combinations "12324ASDFDSFWEGSDFSD",//test case for uppercase and non-character combinations "AASDFDSASDFDSFWEGSDFSD",//measurement
The case of lowercase and uppercase combinations "AASDFDSASDFDSFWEGSDFSD" $@!-=) (&^%#@! ",//test case for lowercase, uppercase, and non-character combinations null,//test empty string Case" "//test string size 0
};
CharCount charcount = new CharCount (); for (String Str:strarray) {System.out.println (String.Format("character assignment in string%s:", str);
Charcount.count (str);
System.out.println ("=================================");
}
}
}