Java Decode Machine Questions

Source: Internet
Author: User

/**
*
* Java writing Encode method and Decode method, machine questions please use java,c,c++
* In any of the languages implement two functions encode () and Decode (), respectively, to implement the transformation and restoration of the string.
* Transform function encode () order to see the characters that know the string, and generate a new string per group according to the following rules:
* (1) If the current character of the string is not known to be a numeric character greater than 0, the character is copied with the new string;
* (2) If the current character of a known string is a numeric character, and he does not have subsequent characters, simply copy it to the new string;
* (3) If the current character of a known string is a numeric character greater than 0, and there are subsequent characters, set the value of the numeric character to N,
* The subsequent character (including the subsequent character is a numeric character) is duplicated n+1 times to the new string; (4) In a group of the above transformations, insert an underscore ' _ ' between different groups for separation;
* (5) If the string contains an underscore ' _ ', then the transform is to use "/ul". For example: the Encode () function transforms the string 24ab_2t2 to
* 444_aaaaa_a_b_/ul_ttt_t_2
*
*
*/
public class Teststringencodedemo {

public static String pub = "";

public static void decode (String str) {

/*
* First operation judgment ' _ ', all subsequent operations are the string after recursion
*/
if (Str.charat (0) = = ' _ ') {
Pub = pub + "/ul" + "_";
} else if ("123456789". IndexOf (Str.charat (0)) = =-1) {//To determine if the character is a numeric type
Pub + = Str.charat (0) + "_";
} else if (str.length () = = 1) {//If the string has only one pop-out method
Pub + = str;
Return
} else {
/*
* "123456789". IndexOf (Str.charat (0)) +1
* Using this method to get the word Poute is the value of the character (since it is all plus 1 from 0 bits) the need to go is the literal value plus 1 all direct +2
*/
for (int i = 0; i < "123456789". IndexOf (Str.charat (0)) + 2; i++) {
Pub + = Str.charat (1);//takes the latter of the current string
}
Pub = pub + "_";
}

Recursively intercepts a string (instead of looping through a string and extracting a value that determines whether the character is an int)
if (Str.length ()! = 1) {
Teststringencodedemo.decode (str.substring (1));
}
}

public static void Encode (String str) {
String pub = "";

for (int i = 0; i < str.length (); i++) {
if (Str.charat (i) = = ' _ ') {//If there is an underscore ' _ ' in the known string, the transform is "/ul"
Pub + = "/ul";
} else if ("123456789". IndexOf (Str.charat (i), 0) = =-1) {//The current character of the string is not a numeric character greater than 0, copy the character with the new string
Pub + = Str.charat (i);
Stitching the last one
} else if ("0123456789". IndexOf (Str.charat (i), 0)! =-1 && i = = (Str.length ()-1)) {
Pub + = Str.charat (i);
}
The current character of the string is a numeric character greater than 0, and there are subsequent characters,
else if ("0123456789". IndexOf (Str.charat (i), 0)! =-1 && i! = Str.length ()-1) {
int pool = Integer.parseint (Str.charat (i) + "");

for (int j = 0; J <= Pool; j + +) {
Pub + = Str.charat (i + 1);
}
}
Pub + = "_";
}
Pub = pub.substring (0, Pub.length ()-1);
SYSTEM.OUT.PRINTLN (pub);
}

public static void Main (string[] args) {
char c = "12345678". CharAt (0);
System.out.println (Character.getnumericvalue (c) + 2);
System.out.println ("12345678". CharAt (0));
String str = "24AB_2TT";

Decode (str);
SYSTEM.OUT.PRINTLN (pub);
Encode (str);
}

}

Java Decode Machine Questions

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.