栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

字符串中找出连续最长的数字字符串的实例代码

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

字符串中找出连续最长的数字字符串的实例代码

复制代码 代码如下:
//1. 写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)
//功能:
//在字符串中找出连续最长的数字串,并把这个串的长度返回,
//并把这个最长数字串付给其中一个函数参数outputstr所指内存。
//例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回9,outputstr所指的值为123456789
#include
#include
int continumax(char *outputstr,char *inputstr)
{   
    assert(outputstr);
    assert(inputstr);
    int length = 0;
    int maxlength = 0; 
    int i = 0;
    int j = 0;
    while(inputstr[i] != '')
    {
        while( inputstr[i] >='0'&& inputstr[i] <= '9')
        {   
            length++;
            i++;
        }
        if(length > maxlength)
        {
            maxlength = length;
            int k = i-maxlength;
            for(j = 0; j < maxlength; j++ )
            {  

                outputstr[j] =inputstr[k++];

            }
            length = 0;
            continue;
        }
        i++;
        length = 0;
    }
        outputstr[j] = '';
        return maxlength;
}

 

int main( )
{   
    char inputstr[ ]= "abcd12345eddafsd125ss123456789";
    char outputstr[100];

    int max_numstr_length = continumax(outputstr,inputstr);
    printf("%sn",outputstr);
    printf("the max_numstr_length is %dn", max_numstr_length);
    return 0;
}

复制代码 代码如下:
#include
#include

 
int continumax(char * outputstr, char * inputstr)
{
    int len = 0;        //统计数字字符串的长度
    int max = 0;        //当前最大数字字符串的长度
    char *pstr =NULL;   //记录最大数字字符的起始位置

 
    while(* inputstr!= '')
    {
        if(*inputstr <= '9' && *inputstr >='0')  //统计数字子字符串的长度
        {
            len++;
            inputstr++;
            continue;
        }
        else if (len > max)        //如果统计出来的数字字符串大于当前的最大数字子字符串的长度,则更新
        {
            max = len;
            pstr = inputstr-len;      
            len = 0;
        }
        inputstr++;
    }
    for(int i = 0 ; i    {
        *outputstr = *pstr;
        outputstr++;
        pstr++;
    }

       outputstr = outputstr-max;
       outputstr[max] ='';
       cout<
       return max;

     
}

int main()
{
    char input[] = "de1234de123456ed";
    //char * out = (char *)malloc(100*sizeof(char));
    char output[100];
    int max = continumax(output, input);
    cout<    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/66192.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号