Huawei Machine Test (string processing collection)

Source: Internet
Author: User
Tags arithmetic function prototype lowercase
Package com.abuge;
Import Java.util.Iterator;

Import Java.util.LinkedHashSet;

Import Org.junit.Test; /** * A string of lowercase letters (A~Z) entered through the keyboard.
  Write a string filter that filters out characters that are not the first occurrence if there are multiple identical characters in the string.
  For example, the string "ABACACDE" filter result is "ABCDE".
  Required implementation function: void Stringfilter (const char *PINPUTSTR, long Linputlen, Char *poutputstr);
  "Input" PINPUTSTR: input string Linputlen: input string Length "output" POUTPUTSTR: output string, space has been opened, and input string equal length; "Note" Only need to complete the function algorithm, the middle does not require any IO Input output example input: "DEEFD" outputs: "def" input: "AFAFAFAF" Output: "AF" Input: "PPPPPPPP" Output: "P" main function has been hidden, here The test entry that is reserved to the user, where you can test your implementation function, and invoke the printf printout you can now use other methods to test, as long as the final program is guaranteed to execute correctly, the function implementation can be arbitrarily modified, but do not change the function prototype.
 Be sure to keep the compilation running unaffected. * @author Abuge * * */public class Getthefilteredstring {public void Stringfilter (String pinputstr, long Linputlen, St
		
		Ring poutputstr) {linkedhashset<character> linkedhashset = new linkedhashset<character> ();
		for (int i = 0; i < Linputlen i++) {Linkedhashset.add (Pinputstr.charat (i));
		StringBuilder sb = new StringBuilder (); Iterator<charactEr> iterator = Linkedhashset.iterator ();
		while (Iterator.hasnext ()) {Sb.append (Iterator.next ());
		} poutputstr = Sb.tostring ();
	System.out.println (POUTPUTSTR);
		@Test public void Test () {String str1 = ' deefd ';
		String str2 = "AFAFAFAF";
		
		String STR3 = "PPPPPPP";
		
		String poutputstr = null;
		Stringfilter (STR1, Str1.length (), POUTPUTSTR);
		Stringfilter (STR2, Str2.length (), POUTPUTSTR);
	Stringfilter (STR3, Str3.length (), POUTPUTSTR);
 }
}
 
<pre name= "code" class= "Java" >package Com.abuge;

Import Org.junit.Test; /** * A string of lowercase letters (A~Z) entered through the keyboard.
  Write a string compression program that compresses the repeated letters that appear in the string and outputs the compressed string. Compression rule: 1, compress only consecutive occurrences of characters.
  For example, the string "ABCBC" because there is no consecutive repeated characters, compressed string or "ABCBC." 2, the Compressed field format is "the number of repeat characters + characters."
  For example, the string "Xxxyyyyyyz" becomes "3x6yz" after compression.
  Required implementation function: void Stringzip (const char *PINPUTSTR, long Linputlen, Char *poutputstr);
  "Input" PINPUTSTR: input string Linputlen: input string Length "output" POUTPUTSTR: output string, space has been opened, and input string equal length;  "Note" Only need to complete the function algorithm, the middle does not need any IO Input output sample input: "CCCDDECC": "3c2de2c" Input: "adef" output: "Adef" Input: "PPPPPPPP" Output: "8p * @author Abuge * */public class Stringzip {public void Stringzip (string pinputstr, Long Linputlen, string poutputstr) {i
		F (pinputstr.isempty () | | | pinputstr = = NULL) return;
		StringBuilder sb = new StringBuilder ();
		int flag = 1;
			for (int i = 0; i < lInputLen-1 i++) {char c = pinputstr.charat (i);
			if (Pinputstr.charat (i) = = Pinputstr.charat (i + 1)) {flag++;
			}else {if (Flag > 1)	{sb.append (flag);
				} sb.append (c);
			flag = 1;
		}///process the last character if (Flag > 1) {sb.append (flag);
		
		} sb.append (Pinputstr.charat (int) (lInputLen-1));
		
		Poutputstr = Sb.tostring ();
	System.out.println (POUTPUTSTR);
		@Test public void Test () {String str1 = ' CCCDDECC ';
		String str2 = "Adef";
		String STR3 = "PPPPPPPP";
		String poutputstring = null;
		Stringzip (STR1, Str1.length (), poutputstring);
		Stringzip (STR2, Str2.length (), poutputstring);
	Stringzip (STR3, Str3.length (), poutputstring);
 }
}




Package com.abuge;

Import Org.junit.Test;
  /** * Through the keyboard input 100 positive integer addition, subtraction, write a program output operation result string.
  The input string is formatted as "operand 1 operator operand 2" and "operand" and "operator" separated by a space.
  Supplemental Note: 1, the operand is a positive integer, do not need to consider the case of overflow calculation results.
  2, if the input formula format error, the output result is "0".
  Required implementation function: void arithmetic (const char *PINPUTSTR, long Linputlen, Char *poutputstr);
  "Input" PINPUTSTR: input string Linputlen: input string Length "output" POUTPUTSTR: output string, space has been opened, and input string equal length; "Note" Only needs to complete the function algorithm, the middle does not need to have any IO Input output example input: "4 + 7" outputs: "11" Input: "4-7" Output: "-3" Input: "9 + + 7" output: "0" NOTE: Malformed * @author Abu GE * */public class Arithmetic {public void arithmetic (string pinputstr, Long Linputlen, string poutputstr) {Str
		ing regex = "[0-9]+[][+-][][0-9]+";
			if (Pinputstr.matches (regex)) {string[] str = pinputstr.split ("");
			INT-A-integer.parseint (str[0]);
			int second = Integer.parseint (str[2]);
			int result = 0;
					Switch (str[1]) {case ' + ': result = second;
				Break
					Case "-": result = First-second;
				Break
			Default:break;
			
	}		Poutputstr = string.valueof (result);
		System.out.println (POUTPUTSTR);
		}else {System.out.println (0);
		@Test public void Test () {String str1 = "29 + 97";
		String str2 = "2-7";
		String STR3 = "9 + + 7";
		String poutputstr = null;
		Arithmetic (str1, Str1.length (), POUTPUTSTR);
		Arithmetic (str2, Str2.length (), POUTPUTSTR);
	Arithmetic (STR3, Str3.length (), POUTPUTSTR);	
 }
}


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.