找出字符串中的最长连续数字串

函数原型:int maxnumstr(char *inputstr, char *outputstr)

函数功能:找出inputstr中的最长连续数字串存储到outputstr里并返回长度,如调用maxnumstr("123abc1234a", outputstr)后返回4,且outputstr中为"1234"。

 

#include <stdio.h>
#include <string.h>
int maxnumstr(char *str,char *des)
{
	int len=0,max=0,flag=0;
	char *c=str,*end=NULL;
	while(*c!='\0')
	{
		if(*c>='0'&&*c<='9')
		{
			if(flag==0)
			{
				flag=1;
				len=1;
			}
			else len++;
			if(len>max)
			{
				max=len;
				end=c;
			}
		}
		else flag=0;
		c++;
	}
	c=end-max+1;
	while(c!=end+1)
	{
		*des=*c;
		des++;
		c++;
	}
	*des='\0';
	return max;
}
int main()
{
	char *str="123abc1234567abddd123",des[100];
	printf("%d\n",maxnumstr(str,des));
	printf("%s\n",des);
	return 0;
}

你可能感兴趣的:(找出字符串中的最长连续数字串)