输出一个字符串中表示数字字符的最长子串(比较简单)
注意:使用指针做参数,可在main中返回
int continumax(char *outputstr, char *inputstr)
{
char *in = inputstr, *out = outputstr, *temp, *final;//temp在这里的作用是很大的。它始终指向当前比较的数字字符串。
int count = 0, maxlen = 0;
while( *in != '\0' )
{
if( *in > 47 && *in < 58 )
{
for(temp = in; *in > 47 && *in < 58 ; in++ )
count++;
}
else
in++;
if( maxlen < count )
{
maxlen = count;
count = 0;
final = temp; // 如果当前temp指向的数字字符子串长度是最大的,那个最终结果指针final直线当前最大子串
}
}
strncpy(out,final,maxlen); // 使用字符串函数,可以节约代码量
*(out+maxlen)='\0';
return maxlen;
}
void main()
{
char *inPtr="ab125cde148962bbbb0";
char *outPtr=(char *)malloc(strlen(inPtr)+1);
continumax(outPtr,inPtr);
puts(outPtr); // 使用字符串函数,可以节约代码量
free(outPtr);
system("pause");
}