求字符串的最大连续递增数字子串长度,如“abcd123456eutr”的连续递增数字子串为123456,长度为6
代码如下:
# include <iostream>
using namespace std;
# include <stdlib.h>
int main()
{
int f(char a[]);
char a[20]="abcd1234578ef";
cout<<f(a)<<endl;
system("pause");
return 0;
}
int f(char a[]) //函数返回最大串的长度
{
int count=0; //数字串的长度
int max=0; //数字串的最大长度
char *p=a;
int flag=1; //数字串的开始标志
while(1)
{
if(*p>='0'&&*p<='9')
{
if(flag)
count=1;
flag=0;
while(1) //寻找最长的数字递增子串
{
if(*(p+1)>='0'&&*(p+1)<='9'&&*(p+1)-*p==1){
p++;
count++;
}
else
{
flag=1;
if(count>max)
max=count;
p++;
break;
}
}
}
else
{
max=count>max?count:max; //更新最大数字串的长度
count=0;
flag=1;
if(*p=='\0')
return max;
p++;
}
}
}