#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char str[80];
int len;//记录当前输入字符串的长度
int p;//p下标用于比较当前字符和下一个字符是否相同;即str[p]==str[p+1]
int q;//q用于生成‘实际是覆盖’下一个字符
int count;
char tmp;
while(scanf("%s",str)!=EOF){
len=strlen(str);
p=q=0;
count=1;
while(p<len){
while(str[p]==str[p+1]){
p++;
count++;
}
if(count>1){
tmp=str[p-1];//这里p已经指向下一个,p-1返回最后一个连续字符
str[q]=count+'0';//这里是让字符i变为整数i
q++;
str[q]=tmp;
count=1;//当前连续字符已处理完,计数器重置
}
else{ //处理不连续的单个字符
str[q]=str[p];
q++;
p++;
}
}
str[q]='\0';
printf("%s\n",str);
}
}
字符串压缩
最新推荐文章于 2025-03-20 10:42:24 发布