String compression--Huawei written test

Source: Internet
Author: User

Second, the title description (40 points):
Enter a string of lowercase letters (A~Z) through the keyboard. Write a string compression program that compresses the repeating letters of successive attendance in a string and outputs the compressed string.
Compression rules:
1. Compress only consecutive occurrences of characters. For example, the string "ABCBC" because there is no continuous repetition of characters, the compressed string is still "ABCBC".
2. The format of the compressed field is "number of characters repeat + characters". For example, the string "XXXYYYYYYZ" is compressed and becomes "3x6yz".

Required implementation functions:
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 up, and input string length;

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

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

My thoughts are:

void Stringzip (const char *PINPUTSTR, long Linputlen, Char *poutputstr)
{
int i,j=0;
int n=0;
for (i=0;i<linputlen;)
{
while (pinputstr[i]==pinputstr[i+1])//Always compare, record the same number of values in N, I always point to the array
{
i++;
n++;
}
poutputstr[j]=n+1+ ' 0 '; Put an integer in a character array with the number of repeating characters n+1
Poutputstr[++j]=pinputstr[i]; Duplicate character Pinputstr[i]
n=0; Operation, these operations are meticulous and prone to problems
i++;
j + +;
}
poutputstr[j]= ' + ';
}

Bumpy road: (1) While loop can not nest while loop, otherwise terminate;

(2) integers can not be placed directly into the character array, or output garbled, need n+ ' 0 ', will be corresponding to the integer transform ASC code;

(3) Output does not reach the ideal situation do not be impatient, this example, if the logic to programming, do not have problems, according to the computer execution sequence and thinking to find errors;

Classic Program:

  • void Stringzip (const char* PINPUTSTR, Long Linputlen, char* poutputstr)
  • {
  • int I, j, K, Num;
  • Char buffer[20];
  • for (i = 0, k = 0; i < Linputlen;)
  • {
  • num = 0;
  • for (j = i + 1; j < Linputlen; ++j)
  • {
  • if (pinputstr[i] = = Pinputstr[j])//The number of repeated occurrences of consecutive letters after each character in the statistics string
  • ++num;
  • Else
  • Break
  • }//for
  • if (0! = num)//num may be an integer of two-bit or three-bit or more bits
  • {
  • memset (buffer, 0, sizeof (buffer));
  • Itoa (num + 1, buffer, 10); Converts an integer to a string by 10
  • strcpy (Poutputstr + k, buffer);
  • K + = strlen (buffer);
  • }
  • poutputstr[k++] = Pinputstr[i];
  • i = i + num + 1;
  • }//for
  • Poutputstr[k] = ' + ';
  • }

String compression--Huawei written test

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.