#include <bits/stdc++.h> using namespace std; void strzip(char* p, char* zp) { int len = strlen(p); int index = 0; for (int i=0;i<len;i++) { int count=1; while (i<len-1 && p[i]==p[i+1]) { count++; i++; } if (count>1&&count<11){ zp[index++]='0'+count; zp[index++]=p[i]; } if (count<=1){ zp[index++]=p[i]; } } } int main() { char str1[100]="HelloWorldddddC++MATLABBBBBBBBBBB"; char str2[100]; strzip(str1,str2); cout<<str2<<endl; return 0; }我想让输出结果为He2loWorl5dC2+MATLA11B 请作出修改
时间: 2023-12-05 18:04:46 浏览: 36
可以在判断字符串中连续相同字符数量的时候,分别判断是否在区间[2,9]和[10,99]中,然后进行对应的转换。代码如下:
```cpp
#include <bits/stdc++.h>
using namespace std;
void strzip(char* p, char* zp) {
int len = strlen(p);
int index = 0;
for (int i=0;i<len;i++) {
int count=1;
while (i<len-1 && p[i]==p[i+1]) {
count++;
i++;
}
if (count>=2 && count<=9) {
zp[index++] = '0' + count;
zp[index++] = p[i];
} else if (count>=10 && count<=99) {
zp[index++] = '0' + count / 10;
zp[index++] = '0' + count % 10;
zp[index++] = p[i];
} else {
zp[index++] = p[i];
}
}
zp[index] = '\0'; // 加上字符串结束符
}
int main() {
char str1[100] = "HelloWorldddddC++MATLABBBBBBBBBBB";
char str2[100];
strzip(str1, str2);
cout << str2 << endl;
return 0;
}
```
输出结果为:
```
He2loWorl5dC2+MATLA11B
```