Huawei machine Test-string compression

Source: Internet
Author: User

Title: String compression

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"


#include <iostream>  using namespace std;    void Stringzip (const char *PINPUTSTR, long Linputlen, char *poutputstr)  {      int count = 1;      int j = 0;            for (int i = 0;i < linputlen-1;i++)      {        if (pinputstr[i] = = pinputstr[i+1])  count++;        else          {              if (Count > 1)            {                  poutputstr[j++] = (char) (count + ' 0 ');//note convert int to char type representation.                  Poutputstr[j++] = Pinputstr[i];                  Count = 1;              }              else poutputstr[j++] = pinputstr[i];  Count and so on 1 of the case.          }      }      POUTPUTSTR[J] = ' + ';  }    int main ()  {      char input[20];     Char output[20];  while (cin>>input) {stringzip (input,20, output);  cout << output << Endl <<endl;  } cout<<endl;          return 0;  }  



Huawei machine Test-string compression

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.