http://blog.csdn.net/zzran/article/details/8008055 2012 要求,給定一串字符串,找出其中最常的數(shù)字字符串。比如,給定字符串a(chǎn)bc123bcd234567d,則最終結(jié)果輸出為234567。
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- char* get_max_num_string(char *str)
- {
- char *begin;
- char *temp,*final;
- int count;
- int maxLen=0;
- begin=str;
- while(*begin!='\0')
- {
- count=0;
- if(*begin>='0'&&*begin<='9')
- {//若此字符為數(shù)字字符,則從此字符開始依次統(tǒng)計(jì)其后數(shù)字字符的個(gè)數(shù)
- for(temp=begin;*begin>='0'&&*begin<='9';begin++)
- count++;
- }
- else
- begin++;
- if(count>maxLen)
- {//用此次的結(jié)果與上次的結(jié)果進(jìn)行比較,保存長度最大的那個(gè)數(shù)字符串的起始地址和長度
- maxLen=count;
- final=temp;
- }
- }
- char* result=(char*)malloc(sizeof(char)*(maxLen+1));
- strcpy(result,final);
- result[maxLen]='\0';
- return result;
- }
- void main()
- {
- char str[]="1234fgrrf56789012dd12345678900";
- char *s=get_max_num_string(str);
- printf("%s\n",s);
- }
|