功能:在字符串中找出连续最长的数字串,并把这个串的长度返回
函数原型:
unsigned int Continumax(char** pOutputstr, char* intputstr)
输入参数:
char* intputstr 输入字符串
输出参数:
char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串
pOutputstr 指向的内存应该在函数内用malloc函数申请,由调用处负责释放
返回值:
连续最长的数字串的长度
函数原型:
unsigned int Continumax(char** pOutputstr, char* intputstr)
输入参数:
char* intputstr 输入字符串
输出参数:
char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串
pOutputstr 指向的内存应该在函数内用malloc函数申请,由调用处负责释放
返回值:
连续最长的数字串的长度
#include <stdlib.h>
int main()
{
unsigned int Continumax(char** , char* );
//char inStr[] = "abcd12345ed125ss123058789";
char inStr[] = "abcd12345ss54761";
//char inStr[] = "fdjndkjyjrthd";
char *OutStr;
int nOut = 0;
nOut = Continumax(&OutStr,inStr);
return 0;
}
unsigned int Continumax(char** pOutputstr, char* intputstr)
{
char *pIn = NULL;
int i = 0, nIn = 0;
int curCnt = 0,maxCnt = 0,curPos = 0;
pIn = intputstr;
while (*pIn != NULL)
{
nIn ++;
pIn++;
}
int nInpnew = nIn+1;
pIn = (char *)malloc(nInpnew*sizeof(char));
for (i=0;i<nIn;i++)
{
pIn[i] = intputstr[i];
}
for (i=0;i<nIn;i++)
{
if ((pIn[i]>='0')&&(pIn[i]<='9'))
{
curCnt += 1;
}
if (!((pIn[i+1]>='0')&&(pIn[i+1]<='9')))
{
if (maxCnt <= curCnt)
{
curPos = i+1;
maxCnt = curCnt;
}
if (pIn[i+1] != '\0')
{
curCnt = 0;
}
}
}
if (maxCnt == 0)
{
*pOutputstr = (char *)malloc(0*sizeof(char));
(*pOutputstr)[0] = '\0';
free(pIn);
return 0;
}
*pOutputstr = (char *)malloc(maxCnt*sizeof(char));
for (i=0;i<maxCnt;i++)
{
(*pOutputstr)[i] = pIn[curPos-maxCnt+i];
}
(*pOutputstr)[i] = '\0';
free(pIn);
return maxCnt;
}
这个写法很笨,还有很大改进空间,待改。