#include
#include
#include
using namespace std;
char* FindNumber(char * out, char *in) {
if (!out || !in)
return NULL;
char *tmp = (char*)malloc(strlen(in) + 1);
int n=0;
strcpy(out, "");
for (int i = 0; in[i] != '\0'; ++i) {
if ((in[i] > '9' || in[i] < '0')&&n!=0) {
tmp[n] = '\0';
n = 0;
if (strlen(out) < strlen(tmp))
strcpy(out, tmp);
}
else {
tmp[n++] = in[i];
}
}
tmp[n] = '\0';
if (strlen(out) < strlen(tmp))
strcpy(out, tmp);
free(tmp);
return out;
}
int main() {
char out[50];
printf(FindNumber(out, "124567812151as123cdf1234f1233456"));
return 0;
}
char* FindNumber(char* out, char* in) { int max = 0; char *begin = in, *end = NULL, *pos = NULL; while (*begin != '\0') { end = begin; while (*end != '\0' && isdigit(*end)) ++end; if (end - begin > max) { max = end - begin; pos = begin; } begin = end > begin ? end : begin + 1; } if (max > 0) { memcpy(out, pos, max); return out; } else return NULL; }
/* 题目描述: 求出一字符串中最长的数字串。比如as123cdf1234f,返回1234,char* FindNumber(char * out,char *in)。 */ #include <iostream> #include <cstdlib> using namespace std; /*判断数字*/ bool isNum(char c) { if (c >= '0' && c <= '9') return true; return false; } char* findNumber(char * out, char *in) { if (!in || !out) return NULL; int maxLen = 0,len = 0; char tmp[100]; for (char *p = in; *p != '\0'; ++p) { if (!isNum(*p)) { if (len > maxLen) { maxLen = len; tmp[len] = '\0'; strcpy(out, tmp); }//if tmp[0] = '\0'; len = 0; }//if else{ tmp[len++] = *p; }//else }//for /*判断最后一个数字串是否最长*/ if (len > maxLen) { maxLen = len; tmp[len] = '\0'; strcpy(out, tmp); }//if return out; } int main() { char *in = new char[100]; char *out = new char[100]; cin >> in; out = findNumber(out, in); cout << out << endl; findNumber(out, in); cout << out << endl; delete in; delete out; system("pause"); return 0; }
char* FindNumber(char * out,char *in) { if(!in || !out) return NULL; char* p = in; char* address = out; string output,str_temp; unsigned int outLen = 0,len_temp = 0; while(*p != '\0') { if(*p >='0' && *p <='9') str_temp += *p; else{ len_temp = str_temp.length(); if(len_temp > outLen) { output = str_temp; outLen = len_temp; } str_temp.clear(); } ++ p; } len_temp = str_temp.length(); if(outLen < len_temp) { output = str_temp; outLen = len_temp; str_temp.clear(); } for(unsigned int i=0;i<outLen;i++) *out++ = output[i]; *out = '\0'; return address; }
char*FindNumber(char*in){vector<char>temp; vector<vector<char>>res;intk=0;for(char*p=out;*p!='\0';p++)if(*p<='9'&& *p>='0')temp[k++]=*p;elseif(temp.size()>0){res.push_back(temp);k=0;}intcount=0;intindex=0;for(inti=0;i<res.size();i++){count=res[i].size();index=i;for(intj=i+1;j<res.size();j++){if(count<res[j].size()){count=res[j].size();index=j;}}}char*out=res[index];returnout;}