字符串中找出持續最長的數字字符串的實例代碼。本站提示廣大學習愛好者:(字符串中找出持續最長的數字字符串的實例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是字符串中找出持續最長的數字字符串的實例代碼正文
//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;
}