代码改变世界

算法100题25

2011-10-06 19:04  justvi  阅读(387)  评论(3编辑  收藏  举报

/* 题目来自:http://blog.csdn.net/v_JULY_v

 * 写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)

 * 功能:

 * 在字符串中找出连续最长的数字串,并把这个串的长度返回,

 * 并把这个最长数字串付给其中一个函数参数outputstr所指内存。

 * 例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回9,

 * outputstr所指的值为123456789

 */

int continuemax(char *outputstr, char *inputstr)
{
char *plong, *pcompare;
char *s = inputstr;
int llen, clen;
llen = clen = 0;
int i;

while (*s != '\0')
{
while (!isdigit(*s))
s++;
if (isdigit(*s))
pcompare = s;
while (isdigit(*s))
{
s++;
clen++;
}
if (clen > llen)
{
llen = clen;
plong = pcompare;
//clen = 0;
}
clen = 0;
}

for (i = 0; i < llen; i++)
outputstr[i] = plong[i];
outputstr[i] = '\0';
return llen;
}

int main()
{
char instr[] = "abcd12345ed125ss123456789";
int len;
char *out = instr;
len = continuemax(out, instr);
printf("%d, %s\n", len, out);
return 0;
}