曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 6049|回复: 0
打印 上一主题 下一主题

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

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34397
跳转到指定楼层
楼主
发表于 2009-6-11 14:09:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
要求
写一个函数,它的原型是:
int findnumstring( char *outputstr, char *inputstr )
功能:在字符串中找出连续最长的数字串,把这个数字串长度返回,并把这个最长的数字串赋给其中一个函数参数 outputstr 所指的内存。例如: "abcd12345eee125ss123456789” 的首地址传给 inputstr 后,函数将返回 9,outputstr 所指的值为 123456789。

参考代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int findnumstring(char *outputstr, char *inputstr)
{
    char *in = inputstr, *out = outputstr, *temp, *final;
    int count = 0, maxlen = 0, i;

    while( *in != '\0' )
    {
        if( *in > 47 && *in < 58 ) {
           
            for(temp = in; *in > 47 && *in < 58; in++)
                count++;
        }
        else
           in++;

        if( maxlen < count ) {
            maxlen = count;
            final = temp;           
        }

        count = 0;
    }

    for(i = 0; i < maxlen; i++)
    {
        *out = *final;
        out++;
        final++;
    }
    *out = '\0';
   
    return maxlen;
}

int main()
{
    char string[] = "abcd12345eee125ss123456789";
    char *p = (char *)malloc( strlen(string) + 1);
    int count = findnumstring(p, string);
   
    printf("%s\nnumber string length = %d\n", p, count);

    return 0;

}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-6-18 07:14 , Processed in 0.092581 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表