转换字符串格式为原来字符串里的字符+该字符连续出现的个数,
例如字符串1233422222,转化为1121324125
#include<iostream>
#include<string>
using namespace std;
int main()
{
cout<<"Enter the numbers "<<endl;
string str;
char reschar[50];
reschar[0]='\0';
cin>>str;
int len=str.length();
int count=1;
int k;
int n=0;
for(k=0;k<len-1;k++)
{
if(str[k+1]==str[k])
count++;
else
{
sprintf(reschar+strlen(reschar),"%c%d",str[k],count);
//reschar[n++]=str[k];reschar[n++]=count+'0'; //这个方法也行
count=1;
}
}
sprintf(reschar+strlen(reschar),"%c%d",str[k],count);
//reschar[n++]=str[k];reschar[n++]=count+'0';reschar[n]='\0';
cout<<reschar<<endl;
return 0;
}