不浮躁
分类: C/C++
2014-12-09 21:49:44
原文地址:找一个字符串中最长的整数 作者:xutianxi
写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)
功能:在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串付给
其中一个函数参数outputstr所指内存。例如:"abcd12345ed125ss123456789"的首地址
传给intputstr后,函数将返回9,outputstr所指的值为123456789
#include
char *continuemax(char *input, int *count)
{
int count_old = 0, count_new = 0;
char *p_old = NULL, *p_new = NULL;
char *s = input;
if (input == NULL)
{
return NULL;
}
while (1)
{
if (*s >= '0' && *s <= '9')
{
if (count_new == 0)
p_new = s;
count_new++;
if (p_old == NULL)
p_old = s;
}
else
{
if (count_new > count_old)
{
count_old = count_new;
p_old = p_new;
}
count_new = 0;
p_new = NULL;
if (*s == '\0')break;
}
s++;
}
*count = count_old;
return p_old;
}
int main()
{
char *p = "adb123ad7899ddada65555555";
char *ret;
int count;
printf("p = %s\n", p);
ret = continuemax(p, &count);
printf("ret = %c, count = %d\n", *ret, count);
return 0;
}