写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)。
功能:
在字符串中找出连续最长的数字串,并把这个串的长度返回,
并把这个最长数字串付给其中一个函数参数outputstr所指内存。
例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回9,
outputstr所指的值为123456789
#include <stdio.h> #include <stdlib.h> #include <string.h> int continumax(char *outputstr, char *intputstr) { int max_num_len = 0; int flag = 0; int i = 0; int count = 0; int area_i = 0; int area_end_i = 0; char *tmp = intputstr; char s1[1024]; while(tmp[i] != '\0') { if(tmp[i] > 47 && tmp[i] < 58) { if(flag == 0) { area_i = i; flag = 1; count++; } else if(flag == 1) { count++; } ++i; } else { if(flag == 1) { if(count > max_num_len) { area_end_i = i; max_num_len = count; int j; for(j = area_i;j < area_end_i;j++) { outputstr[j - area_i] = intputstr[j]; } } count = 0; flag = 0; } ++i; } } if(flag == 1) { if(count > max_num_len) { area_end_i = i; max_num_len = count; int j; for(j = area_i;j < area_end_i;j++) { outputstr[j - area_i] = intputstr[j]; } } count = 0; flag = 0; } return max_num_len; } int main () { char *str = "abd1234xsac2231231xasc111"; char *outputstr = (char *)malloc(1024); int intputstr = continumax(outputstr, str); printf("intputstr = %d\n", intputstr); printf("outputstr = %s\n", outputstr); return 0; } /* continumax(outputstr, str); 这个,只是传入了指针 ,用 continumax(char *outputstr, char *intputstr) 接之后,会退化成指针变量,只能修改malloc里面的值,而不能修改 outputstr指向的位置 continumax(&outputstr, str); 这个传入指针地址,用 continumax(char **outputstr, char *intputstr) 用二级指针接之后,能修改output指向的位置 */