将字符串压缩为:字符连续出现的次数+该字符,如果字符连续出现的次数为1,则保持该字符不变。
如:abbccc 输出 a2b3c
#include<iostream>
#include<string>
using namespace std;
int main()
{
int c=1;
char *p=new char[100];
char *res=new char[100];
char *r=res;
gets(p);
while(*p!='\0')
{
if(*p==*(p+1))
c++;
else
{
if(c!=1)
{
sprintf(res,"%d",c);
res+=strlen(res);
}
sprintf(res,"%c",*p);
res+=strlen(res);
c=1;
}
p++;
}
cout<<r;
}