功能:
在字符串中找出连续最长的数字串,并把这个串的长度返回,
并把这个最长数字串付给其中一个函数参数outputstr所指内存。
例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回9,
outputstr所指的值为123456789
- #include <iostream>
- int continumax(char* outputstr,const char*inputstr)
- {
- int len = 0;
- int maxlen = 0;
- const char *str = inputstr;
- char buf[1024];
- char* tmp=buf;
- for (; *str; )
- {
- if ('0'<=*str && *str <= '9')
- {
- while (*str >= '0' && *str <= '9')
- {
- *tmp++ = *str;
- str++;
- len++;
- }
- *tmp = '\0';
- if (maxlen < len)
- {
- maxlen = len;
- strcpy(outputstr,buf);
- }
- }
- else
- {
- tmp = buf;
- len = 0;
- str++;
- }
- }
- return maxlen;
- }
- int main()
- {
- char inbuf[] = "abcd12345ed125ss123456789";
- char outbuf[1024];
- int len = continumax(outbuf,inbuf);
- printf("%d,%s",len,outbuf);
- return 0;
- }
阅读(1340) | 评论(0) | 转发(1) |