Algorithm and data structure surface questions (19)-Number of characters in a statistical string __java

Source: Internet
Author: User
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 ("=================================");
 }

	}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.