国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

字符串中找出連續(xù)最長的數(shù)字字符串的實例代碼

2020-01-26 15:51:36
字體:
來源:轉載
供稿:網(wǎng)友

復制代碼 代碼如下:

//1. 寫一個函數(shù),它的原形是int continumax(char *outputstr,char *intputstr)
//功能:
//在字符串中找出連續(xù)最長的數(shù)字串,并把這個串的長度返回,
//并把這個最長數(shù)字串付給其中一個函數(shù)參數(shù)outputstr所指內存。
//例如:"abcd12345ed125ss123456789"的首地址傳給intputstr后,函數(shù)將返回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;        //統(tǒng)計數(shù)字字符串的長度
    int max = 0;        //當前最大數(shù)字字符串的長度
    char *pstr =NULL;   //記錄最大數(shù)字字符的起始位置

 
    while(* inputstr!= '/0')
    {
        if(*inputstr <= '9' && *inputstr >='0')  //統(tǒng)計數(shù)字子字符串的長度
        {
            len++;
            inputstr++;
            continue;
        }
        else if (len > max)        //如果統(tǒng)計出來的數(shù)字字符串大于當前的最大數(shù)字子字符串的長度,則更新
        {
            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;
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 钦州市| 康马县| 宁蒗| 全州县| 大姚县| 贡嘎县| 怀集县| 西畴县| 平舆县| 曲麻莱县| 眉山市| 新竹市| 克东县| 烟台市| 金乡县| 五指山市| 错那县| 那坡县| 孝昌县| 沅江市| 太康县| 东莞市| 南川市| 平塘县| 灵璧县| 旬邑县| 临沭县| 西吉县| 岑溪市| 远安县| 巫山县| 黎城县| 阳春市| 廊坊市| 弋阳县| 神农架林区| 仁怀市| 定结县| 巩义市| 防城港市| 商河县|