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