找一个字符串中最长的整数

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

#include <stdio.h>

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值