求最大连续递增数字串(如―ads3sl456789DF3456ld345AA‖中的―456789‖)
#include <iostream>
using namespace std;
int getSubString(char src[], char rst[])
{
/*
cout<<rst<<endl;
cout<<"the length of the rst is : "<<sizeof(rst)<<endl;
rst[2]='\0';
cout<<rst<<endl;
cout<<"the length of the rst is : "<<sizeof(rst)<<endl;*/
int pStart = -1, pLen = -1;
int cStart = -1;
int Index = 0, NIndex = 1;
int cnt;
while (src[NIndex] != '\0')
{
cnt = 1;
bool isflag = true;
while(src[Index] >= '0' && src[Index] <= '9' && src[NIndex] <='9' && src[NIndex] == src[Index] + 1)
{
if (isflag)
{
cStart = Index;
isflag = false;
}
NIndex++;
Index++;
cnt++;
}
if (cnt > pLen)
{
pLen = cnt;
pStart = cStart;
}
NIndex++;
Index++;
}
int j = 0;
for (int i = pStart; i < pLen+pStart; i++) //之前用for(int i = pStart, j =0; i < pLen+pStart; i++)出现了错误,因为这里相当于重新定义了int j
{
rst[j++] = src[i];
}
rst[j]='\0';
return pLen;
}
int main()
{
/*
char test[] = "12345";
test[3] = '\0';
cout<<test<<endl;*/
char strSource[]="abs3sl456789DF3456ld345AA";
char strResult[sizeof(strSource)];
cout<<getSubString(strSource, strResult)<<" "<<strResult<<endl;
}