Enter a string of lowercase letters (~ Z. Compile a character string compression program to compress duplicate letters that are present continuously in the string, And output the compressed string. Compression rules: 1. Only the characters that appear repeatedly are compressed. For example, string "abcbc" The compressed string is still "abcbc" . 2. The format of the compressed field is "Repeated characters + characters" . Example: String "xxxyyyyyyz" After compression, it becomes "3x6yz"
# Include <iostream> # include <string. h> using namespace STD; int main () {cout <... "<Endl; char STR [100] =" "; char outstr [100] =" "; cin. getline (STR, 100); int Len = strlen (STR); int I = 0, j = 0; int CNT = 1; char c = STR [0]; for (I = 1; I <Len; I ++) {If (STR [I] = c) {CNT ++;} else {If (CNT = 1) {outstr [J ++] = C;} else {outstr [J ++] = CNT + '0'; outstr [J ++] = C ;} C = STR [I]; CNT = 1 ;}} if (C! = STR [J-1]) {If (CNT = 1) {outstr [J ++] = C;} else {outstr [J ++] = CNT + '0 '; outstr [J ++] = C ;}} cout <outstr <Endl; return 0 ;}
String compression for questions on Huawei Machine