字符串“abbcccdddd”输出为“ab2c3d4”的形式,C 或C++如何实现

如题所述

第1个回答  2012-10-30
#include <stdio.h>
int main()
{
char c,tmp;
int i=0;
for(c=getchar();c!='\n';)
{
tmp=c;
if(!i)
putchar(c);
c=getchar();
if(c==tmp)
i++;
else if(i!=0)
{
printf("%d",i+1);
i=0;
}
}
return 0;
}
求采纳,谢谢本回答被提问者和网友采纳
第2个回答  2012-10-30
对字符c[i],如果c[i]==c[i+1],那么n++,否则输出n并且n=0。
第3个回答  2012-10-30
#include<stdio.h>
int main()
{
char str[11]="abbcccdddd",c;
int i=0,j=0,temp=0;
while(i<9)
{
c = str[i];
printf("%c",c);
for(j=i;j<=10;j++)
{
if(str[j] == c)
{
temp++;
}
else
{
if(temp>1) printf("%d",temp);
i=j;j=10;temp=0;
}
}
}
return 0;
}
第4个回答  2012-10-30
代码已经调试
#include <stdio.h>
#include <string.h>
#define DTR_LENGTH 256

int str_switch(const char *pstr_in, char *pstr_out, int length);
int main()
{
char str_in[DTR_LENGTH], str_out[DTR_LENGTH];

printf("Input:\n");
gets(str_in);

str_switch(str_in, str_out, DTR_LENGTH);

printf("Output:\n");
puts(str_out);
return 0;
}

int str_switch(const char *pstr_in, char *pstr_out, int length)
{
if (pstr_in == NULL || pstr_out == NULL)
return -1;

memset(pstr_out, 0, length);

int count;
const char *pp_in = pstr_in, *pn_in;
char *p_out = pstr_out;

while(*pp_in != '\0') {
*p_out++ = *pp_in;

pn_in = pp_in + 1;
count = 1;
while(*pn_in == *pp_in) {
count ++;
pn_in++;
}
*p_out++ = '0' + count;
pp_in = pn_in;
}
return 0;
}
相似回答