学C语言难得过老外学用中国的筷子吗?
1.
void Press(char *str)
{
int count=1;
int i=0,j=0,k=0,m=0;
char data[100];
int b[10];
while(str[i]!='\0')
{
count=1;
j=i+1;
while(str[j]!='\0'&&str[i]==str[j])
{
j++;
count++;
}
data[k++]=str[i];
m=0;
while(count) //将个数转换为字符
{
b[m++]=count%10;
count/=10;
}
while(m--)
{
data[k++]=b[m]+'0';
}
i=j;
}
data[k]='\0';
printf("\n%s\n%s\n",str,data);
}
感觉空间浪费的太大了,本来想直接移来着,不过想想时间效率不高,也很麻烦.所以就将就一下了
void Press(char *str)
{
int count=1;
int i=0,j=0;
char data;
int b;
while(str[i]!='\0')
{
count=1;
j=i+1;
while(str[j]!='\0'&&str[i]==str[j])
{
j++;
count++;
}
i=j;
}
printf("%s",str[i]);
if(count!==1)
printf("%d",count);
}