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

所属分类: 软件编程 / C 语言 阅读数: 58
收藏 0 赞 0 分享

复制代码 代码如下:

//1. 写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)
//功能:
//在字符串中找出连续最长的数字串,并把这个串的长度返回,
//并把这个最长数字串付给其中一个函数参数outputstr所指内存。
//例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回9,outputstr所指的值为123456789
#include<stdio.h>
#include<assert.h>
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] != '\0')
    {
        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] = '\0';
        return maxlength;
}

 

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

    int max_numstr_length = continumax(outputstr,inputstr);
    printf("%s\n",outputstr);
    printf("the max_numstr_length is %d\n", max_numstr_length);
    return 0;
}

复制代码 代码如下:

#include<iostream.h>
#include<malloc.h>

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

 
    while(* inputstr!= '\0')
    {
        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<max;i++)  //将最大子字符串的值拷贝给outputstr
    {
        *outputstr = *pstr;
        outputstr++;
        pstr++;
    }

       outputstr = outputstr-max;
       outputstr[max] ='\0';
       cout<<outputstr<<endl;

       return max;

     
}

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

更多精彩内容其他人还在看

C++广播通信实例

这篇文章主要介绍了C++实现广播通信的方法,实例讲述了C++ socket广播通信的原理与实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

C++计算ICMP头的校验和实例

这篇文章主要介绍了C++计算ICMP头的校验和的方法,代码简单实用,对于校验ICMP报文来说有不错的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++设置超时时间的简单实现方法

这篇文章主要介绍了C++设置超时时间的简单实现方法,涉及系统函数setsockopt对套接口的操作,具有一定的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++实现ping程序实例

这篇文章主要介绍了C++实现ping程序实例,涉及C++对于ICMP数据包的发送与回显处理,具有一定的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++之boost::array的用法

这篇文章主要介绍了C++之boost::array的用法,以实例的形式简单讲述了静态数组的容器boost::array的使用技巧,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++之Boost::array用法简介

这篇文章主要介绍了C++之Boost::array用法简介,较为详细的分析了Boost::array中的常见用法,并用实例的形式予以总结归纳,需要的朋友可以参考下
收藏 0 赞 0 分享

VC文件目录常见操作实例汇总

这篇文章主要介绍了VC文件目录常见操作实例汇总,总结了VC针对文件目录的各种常用操作,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

VC打印word,excel文本文件的方法

这篇文章主要介绍了VC打印word,excel文本文件的方法,是VC操作文本文件中非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

VC++获得当前进程运行目录的方法

这篇文章主要介绍了VC++获得当前进程运行目录的方法,可通过系统函数实现该功能,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

VC中SendMessage和PostMessage的区别

这篇文章主要介绍了VC中SendMessage和PostMessage的区别,较为全面的分析了SendMessage和PostMessage运行原理及用法上的不同之处,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多