在字符串中找出连续最长的数字串
功能:在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串付给其中一个函数参数outputstr所指内存。例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回9,outputstr所指的值为123456789
#include <stdio.h>
int Findmaxlen(char *input,char *output);
void main()
{
char input[]="abc123def123456ee123456789dd";
char output[50]={0};
int maxlen;
maxlen=Findmaxlen(input,output);
printf("the str %s\n",output);
printf("the maxlen is %d \n",maxlen);
int Findmaxlen(char *input,char *output);
void main()
{
char input[]="abc123def123456ee123456789dd";
char output[50]={0};
int maxlen;
maxlen=Findmaxlen(input,output);
printf("the str %s\n",output);
printf("the maxlen is %d \n",maxlen);
}
int Findmaxlen(char *input,char *output)
{
int Findmaxlen(char *input,char *output)
{
char *in=input,*out=output,*temp,*final;
int count=0,maxlen=0,i;
while(*in!='\0')
{
if(*in>='0'&&*in<='9')
{
count=0;
for(temp=in;*in>='0'&&*in<='9';in++)
count++;
if(maxlen<count)
{
maxlen=count;
final=temp;
}//if
}//if
in++;
}//while
for(i=0;i<maxlen;i++)
*out++=*final++;
*out='\0';
return maxlen;
}
int count=0,maxlen=0,i;
while(*in!='\0')
{
if(*in>='0'&&*in<='9')
{
count=0;
for(temp=in;*in>='0'&&*in<='9';in++)
count++;
if(maxlen<count)
{
maxlen=count;
final=temp;
}//if
}//if
in++;
}//while
for(i=0;i<maxlen;i++)
*out++=*final++;
*out='\0';
return maxlen;
}