一道具有挑战性的题
------ 就这标题而言,若给你写了代码,那岂不把自己当作二愣子
------ 就这标题而言,若给你写了代码,那岂不把自己当作二愣子
程序代码:
int mycompress(const char * strin, char * strout)
{
char curChar = '\0';
unsigned int i = 0, j=0;
unsigned int charCount = 0;
for(i=0; strin[i]!='\0'; i++)
{
if((strin[i]>='a' && strin[i]<='z') || (strin[i]>='A' && strin[i]<='Z'))
{
//current count
if(curChar == strin[i])
{
charCount++;
}
else
{
//save old count
if(curChar != '\0')
{
if(charCount > 1)
{
j += sprintf(&strout[j], "%c%u", curChar, charCount);
}
else
{
j += sprintf(&strout[j], "%c", curChar);
}
}
//begin new count
curChar = strin[i];
charCount=1;
}
}
else
{
fprintf(stderr, "format error! %c is not allowed!\n", strin[i]);
return -1;
}
}
if(curChar != '\0')
{
j += sprintf(&strout[j], "%c%u", curChar, charCount);
}
strout[j] = '\0';
return 0;
}
int main(int argc, char * argv[])
{
char strin[] = "eeeeeaaaff";
char strout[sizeof(strin)];
mycompress(strin, strout);
printf("%s %s\n", strin, strout);
return 0;
}