Java implementation compressed string and Java string filter _java

Source: Internet
Author: User

Topic One: Input a string of lowercase letters (A~Z) 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:

Copy Code code as follows:

void Stringfilter (const char *PINPUTSTR, long Linputlen, Char *poutputstr);

"Enter" PINPUTSTR: input string
Linputlen: Input string length
"Output" POUTPUTSTR: output string, space has been opened up, with the input string equal length;

"Note" Only need to complete the function algorithm, the middle does not require any IO input and output

Example
Input: "DEEFD" Output: "Def"
Input: "AFAFAFAF" Output: "AF"
Input: "PPPPPPPP" Output: "P"

The main function is already hidden, and this is reserved for the user's test entry, where you can test your implementation function to call printf printout
Currently you can use other methods to test, as long as the final program is guaranteed to execute correctly, the function implementation can be arbitrarily modified,
But don't change the function prototype. Be sure to keep the compilation running unaffected.

Title Two: Topic Description:

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 format of the compressed field is "number of characters repeated + characters". For example: the string "XXXYYYYYYZ" is compressed to become "3x6yz"

Required implementation function:

Copy Code code as follows:

void Stringzip (const char *PINPUTSTR, long Linputlen, Char *poutputstr);

"Enter" PINPUTSTR: input string
Linputlen: Input string length
"Output" POUTPUTSTR: output string, space has been opened up, with the input string equal length;

"Note" Only need to complete the function algorithm, the middle does not require any IO input and output

Example
Input: "CCCDDECC" output: "3c2de2c"
Input: "adef" output: "Adef"
Input: "PPPPPPPP" output: "8p"

Copy Code code as follows:

public class Test {



static void Stringfilter (char inputstr[], long Len, Char outputstr[]) {
Int[] a= new int[26];
int num=0;
int j=0;//outputstr[] Count
for (int i=0;i<len;i++) {
num = inputstr[i]-' a ';
if (a[num]==0) {//not seen
Outputstr[j]=inputstr[i];
j + +;
A[num]=1;
}
}
}

static void Stringzip (char inputstr[], long Len, Char outputstr[]) {
Char temp= ';
int num=1;
int point=0;
for (int i=0;i<len;i++) {
if (inputstr[i]==temp) {
num++;
}else{
if (num!=1) {
Outputstr[point++]= (char) (num+ ' 0 ');
Num=1;
}
Outputstr[point++]=temp;
Temp=inputstr[i];
}
}
}

public static void Main (string[] args) {
TODO auto-generated Method Stub
Char [] test={' a ', ' a ', ' a ', ' C ', ' B ', ' B ', ' B ', ' A ', ' a ', ' C ', ' a ', ' d ', ' d ', ' d ', ' C ', ' d ', ' e '};
Long len=test.length;
char [] res = new char[(int) Len];
Stringfilter (Test,len,res);
int j=res.length;
for (int i=0;i<j;i++) {
if (res[i]!= ' ") {
System.out.print (Res[i]);
}else
Break
}

Char[] res2=new char[(int) (2*len)];
Stringzip (Test,len,res2);
for (int i=0;i<2*len;i++) {
if (res2[i]!= ' ") {
System.out.print (Res2[i]);
}else
Break
}
}
}

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.