找出连续最长的数字串

jadyzdr 2010-04-13 01:43:02
写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)
功能: 在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串付给其中一个函数参数outputstr所指内存。例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回outputstr所指的值为123456789
...全文
119 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2010-04-13
  • 打赏
  • 举报
回复
《编译原理》词法分析-有限状态自动机
hacker1125 2010-04-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wade_2003 的回复:]
我觉得可以用 isdigit()这个函数来实现吧
[/Quote]

可以的,isdigit()和 c >= '0' && c <= '9'是一样
wade_2003 2010-04-13
  • 打赏
  • 举报
回复
我觉得可以用 isdigit()这个函数来实现吧
PapaDog_ 2010-04-13
  • 打赏
  • 举报
回复
友情UP
dskit 2010-04-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 we_sky2008 的回复:]
电脑上没调试工具,可能会有错

C/C++ code

int continumax(char *outputstr, const char *intputstr)
{
int cnt = 0, cnt_max = 0;
const char *p;

while(*intputstr)
{
if ('0' <= *intput……
[/Quote]
就是这个思路了
we_sky2008 2010-04-13
  • 打赏
  • 举报
回复
电脑上没调试工具,可能会有错

int continumax(char *outputstr, const char *intputstr)
{
int cnt = 0, cnt_max = 0;
const char *p;

while(*intputstr)
{
if ('0' <= *intputstr && *intputstr <= '9')
{
cnt = 0;
p = intputstr;
while ('0' <= *intputstr && *intputstr <= '9')
{
cnt++;
intputstr++;
}
if (cnt > cnt_max)
{
cnt_max = cnt;
memcpy(outputstr, p, cnt);
*(outputstr + cnt) = 0;
}
}
else
intputstr++;
}
return cnt_max;
}
zzmlake 2010-04-13
  • 打赏
  • 举报
回复
大概写一下,可能有些没考虑到的,随便看看吧

int continumax(char *outputstr,char *intputstr)
{
if(!intputstr || !outputstr) return 0;

char * p1 = intputstr;
char * p2;
char * p3;
int len2 = 0;
int len3 = 0;

while (*p1 !='\0')
{
if (*p1<'0' || *p1>'9' )
{
p1++;
continue;
}

p2=p1;
while (*p1>='0' && *p1<='9' && *p1 != '\0')
{
p1++;
len2++;
}

if (len2>len3)
{
p3=p2;
len3=len2;
}

len2 = 0;
}

memcpy(outputstr, p3, len3);
return len3;

}
LeonTown 2010-04-13
  • 打赏
  • 举报
回复
O(n)的比较吧。。。

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧