/*
copyright@nciaebupt 轉載請註明出處
題目:在字符串中找出連續最長的數字串,並把這個串的長度返回,並把這個最長數字串付
給其中一個函數參數outputstr所指內存。例如:"abcd12345ed125ss123456789"的首地址傳給
intputstr 後,函數將返回9,outputstr所指的值爲123456789
*/
#include <cstdlib>
#include <iostream>
#include <cstring>
int findMaxNumString(char * str, char * outputstr){
if(str == NULL) return -1;
int count = 0;
int maxlen = 0;
char * numstr;
for(int i = 0; i < strlen(str); ++i){
if(str[i] >= '0' && str[i] <= '9'){
count = 0;
for(int j = i; j < strlen(str); ++j){
if(str[j] >= '0' && str[j] <= '9'){
count++;
}
else{
break;
}
}
if(maxlen < count){
maxlen = count;
numstr = &str[i];
}
}
}
char * out = outputstr;
for(int i = 0; i < maxlen; ++i){
*out++ = *numstr++;
}
*out = '\0';
return maxlen;
}
int main(int argc, char ** argv){
char str[] = "abcd12345ed125ss123456789";
char outputstr[100];
int len = findMaxNumString(str, outputstr);
std::cout<<len<<std::endl;
std::cout<<outputstr<<std::endl;
system("pause");
return 0;
}
在字符串中找出連續最長的數字串,並把這個串的長度返回
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.