Chinaunix首页 | 论坛 | 博客
  • 博客访问: 399100
  • 博文数量: 101
  • 博客积分: 2207
  • 博客等级: 大尉
  • 技术积分: 2508
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-19 20:45
文章分类

全部博文(101)

文章存档

2013年(15)

2012年(86)

我的朋友

分类: C/C++

2012-09-25 17:43:44

功能:
在字符串中找出连续最长的数字串,并把这个串的长度返回,
并把这个最长数字串付给其中一个函数参数outputstr所指内存。


例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回9,
outputstr所指的值为123456789

点击(此处)折叠或打开

  1. #include <iostream>
  2. int continumax(char* outputstr,const char*inputstr)
  3. {
  4.     int len = 0;
  5.     int maxlen = 0;
  6.     const char *str = inputstr;
  7.     char buf[1024];
  8.     char* tmp=buf;
  9.     for (; *str; )
  10.     {
  11.         if ('0'<=*str && *str <= '9')
  12.         {
  13.             while (*str >= '0' && *str <= '9')
  14.             {
  15.                 *tmp++ = *str;
  16.                 str++;
  17.                 len++;
  18.             }
  19.             *tmp = '\0';
  20.             if (maxlen < len)
  21.             {
  22.                 maxlen = len;
  23.                 strcpy(outputstr,buf);
  24.             }

  25.         }
  26.         else
  27.         {
  28.             tmp = buf;
  29.             len = 0;
  30.             str++;
  31.         }
  32.     }
  33.     return maxlen;
  34. }
  35. int main()
  36. {
  37.     char inbuf[] = "abcd12345ed125ss123456789";
  38.     char outbuf[1024];
  39.     int len = continumax(outbuf,inbuf);
  40.     printf("%d,%s",len,outbuf);
  41.     return 0;
  42. }

阅读(1303) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~