Chinaunix首页 | 论坛 | 博客
  • 博客访问: 848059
  • 博文数量: 156
  • 博客积分: 6553
  • 博客等级: 准将
  • 技术积分: 3965
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-22 18:36
文章存档

2012年(3)

2011年(43)

2010年(110)

分类: C/C++

2010-10-20 14:34:02

写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)
功能:在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串付给
其中一个函数参数outputstr所指内存。例如:"abcd12345ed125ss123456789"的首地址
传给intputstr后,函数将返回9,outputstr所指的值为123456789

#include

char *continuemax(char *input, int *count)
{
 int count_old = 0, count_new = 0;
 char *p_old = NULL, *p_new = NULL;
 char *s = input;
 if (input == NULL)
 { 
  return NULL;  
 }
 
 while (1)
 {
  if (*s >= '0' && *s <= '9')
  {
   if (count_new == 0)
    p_new = s;
   count_new++;
   if (p_old == NULL)
    p_old = s;
  }
  else
  {
   if (count_new > count_old)
   {
    count_old = count_new;
    p_old = p_new;
   }
   count_new = 0;
   p_new = NULL;
   if (*s == '\0')break;
  }
  s++;
 }
 *count = count_old;

 return p_old;
}

int main()
{
 char *p = "adb123ad7899ddada65555555";
 char *ret;
 int count;

 printf("p = %s\n", p);
 ret = continuemax(p, &count);

 printf("ret = %c, count = %d\n", *ret, count);

 return 0;
}

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