Algorithm and data structure surface question (21)-Finds the first occurrence of a character in a string __java

Source: Internet
Author: User
Tags lowercase
Topics


Finds the first occurrence of a character in a string. If you enter Abaccdeff, then output B.

Thinking of solving problems


The last question is to find the number of characters in the string, in fact, this problem is more simple than the previous question, only need to get the first 1 on the end of the traversal. Relatively easy. But some of the previous code needs to be modified


public class CharCount {public void count (String str) {if (str = = NULL | | str.length () = = 0) {return;
		} StringBuilder builder = new StringBuilder (Str.length ());
		Iscontainschar (str, str.length (), builder);
			if (builder.length () = = 0) {System.out.println ("no characters");
		Return }//Ordered HashMap to count the number of characters, time complexity is O (n) map<character, integer> countmap = new Linkedhashmap<character, INTEGER&G
		t; ();
			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);
			Traverse Map if (!countmap.containsvalue (1)) {System.out.println ("The string does not contain only one occurrence of the character");
		Return
		} set<map.entry<character, integer>> Set = Countmap.entryset ();
		Iterator<entry<character, integer>> Iterator = Set.iterator (); while (ITERATOR.HASNExt ()) {entry<character, integer> Entry = Iterator.next ();
				if (entry.getvalue () = = 1) {System.out.println ("first occurrence only once characters are:" + entry.getkey ());
			Return
		}}} private void Iscontainschar (String str, int length, StringBuilder builder) {str = str.tolowercase ();
		Pattern pattern = Pattern.compile ("[a-z]+");
		Matcher Matcher = Pattern.matcher (str);
		int k = 0;
				while (K < length) {if (Matcher.find ()) {Builder.append (Matcher.group ());
					if (matcher.end () = = length) {//System.out.println ("over");
				Break
		}} k++; }} private void Printcountinfo (Map<character, integer> Map) {if (!map.isempty ()) {Set<entry<charact
			Er, 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 the case of no characters, can distinguish the number "" $@!-= (&^%#@! ",//test without characters, can distinguish the special symbol" Asdfsewrewe ",//test lowercase characters of the statistics" ASDFDSFWEGSDFSD ",/ /test the statistics "12324AASDFDS" of uppercase characters,//test case of lowercase characters and non-character combinations "12324ASDFDSFWEGSDFSD",//test case for uppercase and non-character combinations "Aasdfdsasdfdsfwegsdfs D ",//test case for lowercase and uppercase combinations" AASDFDSASDFDSFWEGSDFSD "$@!-=) (&^%#@!",//test case for lowercase, uppercase, and non-character combinations null,//test empty strings ""//Test
		The case of a string size of 0};
		CharCount charcount = new CharCount ();
			for (string str:strarray) {System.out.println (character assignment in string%s: ", str));
			Charcount.count (str);
		System.out.println ("================================="); }

	}
}

Executing program output results


Character assignment in string 123456: No characters ================================= string "$@!-=" (character assignment in &^%#@!: no characters ===================== ============ character Assignment in string Asdfsewrewe: Contains characters: Asdfsewrewe a:1 s:2 d:1 f:1 e:3 w:2 r:1 The first character that appears only once is: a ======= ========================== character Assignment in string ASDFDSFWEGSDFSD: Contains characters: ASDFDSFWEGSDFSD a:1 s:4 d:4 f:3 w:1 e:1 g:1 First The characters that appear only once are: a ================================= character assignment in string 12324aasdfds: Contains characters: Aasdfds a:2 s:2 d:2 f:1 The first occurrence of a word  Characters: F ================================= character Assignment in string 12324ASDFDSFWEGSDFSD: Contains characters: ASDFDSFWEGSDFSD a:1 s:4 d:4 f:3 W : 1 e:1 g:1 The first character that appears only once is: a ================================= character assignment in a string AASDFDSASDFDSFWEGSDFSD: Contains characters: AASDFDSASDFD SFWEGSDFSD a:3 s:6 d:6 f:4 w:1 e:1 g:1 The first character that appears only once is: W ================================= string Aasdfdsasdfds   FWEGSDFSD "$@!-=" (character assignment in &^%#@!: Contains characters: AASDFDSASDFDSFWEGSDFSD a:3 s:6 d:6 f:4 w:1 e:1 g:1 The first word that appears only once identifier : w ================================= character assignment in string null: ================================= character Assignment in string: =================================
 




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.