#include <stdlib.h>
#include <string.h>
#include "oj.h"
/* 功能:在字符串中找出连续最长的数字串,并把这个串的长度返回
函数原型:
unsigned int Continumax(char** pOutputstr, char* intputstr)
输入参数:
char* intputstr 输入字符串
输出参数:
char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串
pOutputstr 指向的内存应该在函数内用malloc函数申请,由调用处负责释放
返回值:
连续最长的数字串的长度
*/
unsigned int Continumax(char** pOutputstr, char* intputstr)
{
char *temp_input=intputstr;
char **temp_output=pOutputstr;
int flag=0,cur_count=0,max_count=0,max_pos=0,cur_pos=0;
char pre_str;
if(!intputstr||!(*pOutputstr))
return 0;
while(*temp_input!='\0')
{
if(*temp_input>'9'||*temp_input<'0')
{
flag=0;
if(max_count<cur_count)
{
max_pos=cur_pos-cur_count;
max_count=cur_count;
}
cur_count=0;
}
else
{
if(flag==0)
{
cur_count=1;
pre_str=*temp_input;
}
else if(0==pre_str-*temp_input)
{
cur_count=1;
}
else if(flag==1&&pre_str-*temp_input==1)
{
cur_count++;
pre_str=*temp_input;
}
else if(flag==-1&&*temp_input-pre_str==1)
{
cur_count++;
pre_str=*temp_input;
}
else
{
cur_count=1;
pre_str=*temp_input;
}
}
cur_pos++;
temp_input++;
}
if(max_count<cur_count)
{
max_pos=cur_pos-cur_count;
max_count=cur_count;
}
*temp_output=(char *)malloc(max_count*sizeof(char)+1);
if(max_count==0)
**temp_output='\0';
else
strncpy(*temp_output,intputstr,cur_count);
return max_count;
}
温馨提示:答案为网友推荐,仅供参考