文档库 最新最全的文档下载
当前位置:文档库 › 在字符串中找出连续最长的数字串+

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

/*
问题描述:
————————————————————————
描述: 题目描述



请一个在字符串中找出连续最长的数字串,并把这个串的长度返回;如果存在长度相同的连续数字串,返回最后一个连续数字串;

注意:数字串只需要是数字组成的就可以,并不要求顺序,比如数字串“1234”的长度就小于数字串“1359055”,如果没有数字,则返回空字符串(“”)而不是NULL!

样例输入

abcd12345ed125ss123058789

abcd12345ss54761



样例输出

输出123058789,函数返回值9

输出54761,函数返回值5





接口说明

函数原型:

unsignedint Continumax(char** pOutputstr, char* intputstr)

输入参数:
char* intputstr 输入字符串;

输出参数:
char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串;如果输入字符串是空,也应该返回空字符串;

返回值:
连续最长的数字串的长度




知识点:



题目来源: 杜中国
维护人: l00214399
练习阶段: 初级
最后修改时间: 2014-03-17 17:43:34 (如果在此时间前下载过试题,请重新下载)


————————————————————————
*/

程序代码:
————————————————————————

#include
#include "oj.h"


/* 功能:在字符串中找出连续最长的数字串,并把这个串的长度返回
函数原型:
unsigned int Continumax(char** pOutputstr, char* intputstr)
输入参数:
char* intputstr 输入字符串
输出参数:
char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串
pOutputstr 指向的内存应该在函数内用malloc函数申请,由调用处负责释放

返回值:
连续最长的数字串的长度

*/
unsigned int Continumax(char** ppOutputstr, char* intputstr)
{
if(NULL == ppOutputstr || NULL == intputstr)
return -1;

int count = 0;
int max= 0;

char * p, *q;
p = q = intputstr;

while(*p)
{
while(*p >= '0' && *p <= '9')
{
count++;
p++;

}//while

if(count > 0 && count >= max)
{
q = p - count;
max = count;
}//if

count = 0;//【】计数器复位【】
if(*p)//【】判断是否结束,以免操作结束,注意这里不是指针判空【】
p++;//【】递增遍历【】
}//while

if(0 == max)
{
char *pstr = (char *)malloc(1);
pstr = "";
*ppOutputstr = pstr;
}
else
{
char *pstr = (char *)malloc(max + 1);
for(int i = 0; i < max; i++)
{
*(pstr + i ) = *(q + i);
}
*(pstr + max) = '\0';

*ppOutputstr = pstr;
}

return max;
}

相关文档
相关文档 最新文档