題目:輸入一個字符串,找出字符串中最長數字
#include<stdio.h>char num_long(char *str); int main(void){ char str[100]; PRintf("Please input a string with numbers:/n"); scanf("%[^/n]",str); num_long(str); //調用查找最長數字串函數}char num_long(char *str){ int loc = 0,max_loc = 0; int len = 0,max_len = 0; int flag = 0; int i = 0; while(str[i] != '/0') { if(str[i] >= '0' && str[i] <= '9') { if(flag == 0) //標志是否是數字 { flag = 1; loc = i; //記錄數字串首地址 } len++; //記錄字符串長度 } else { if(flag == 1) { flag = 0; if(max_len < len) //比較出最長字符串 { max_len = len; max_loc = loc; } len = 0; //置0,以便記錄下一個數字串長度 } } i++; } for(i = max_loc; i < (max_loc + max_len); i++) { printf("%c",str[i]); }}串
新聞熱點
疑難解答