|
要求:
写一个函数,它的原型是:int findnumstring( char *outputstr, char *inputstr ) 功能:在字符串中找出连续最长的数字串,把这个数字串长度返回,并把这个最长的数字串赋给其中一个函数参数 outputstr 所指的内存。例如: "abcd12345eee125ss123456789” 的首地址传给 inputstr 后,函数将返回 9,outputstr 所指的值为 123456789。
参考代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int findnumstring(char *outputstr, char *inputstr)
{
char *in = inputstr, *out = outputstr, *temp, *final;
int count = 0, maxlen = 0, i;
while( *in != '\0' )
{
if( *in > 47 && *in < 58 ) {
for(temp = in; *in > 47 && *in < 58; in++)
count++;
}
else
in++;
if( maxlen < count ) {
maxlen = count;
final = temp;
}
count = 0;
}
for(i = 0; i < maxlen; i++)
{
*out = *final;
out++;
final++;
}
*out = '\0';
return maxlen;
}
int main()
{
char string[] = "abcd12345eee125ss123456789";
char *p = (char *)malloc( strlen(string) + 1);
int count = findnumstring(p, string);
printf("%s\nnumber string length = %d\n", p, count);
return 0;
} |
|