写一个函数找出连续最长的数字串

本文介绍了一个简单的C++程序,该程序用于从给定的字符串中找出最长的连续数字序列,并将其复制到另一个字符数组中。同时,程序还返回了最长连续数字序列的长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单题。。 

  1 #include <iostream>
  2 using namespace std;
  3 int continueNum( char *str, char *intput )
  4 {
  5     int n = 0, maxcount = 0, count = 0;
  6     char *p = str, *maxp = NULL, *indexp = NULL;
  7     while( *p != '\0' )
  8     {
  9         if( *p >= '0' && *p <= '9' )
 10         {
 11             if( count++ == 0)indexp = p;
 12             if(count > maxcount)
 13             {
 14                 maxp = indexp;
 15                 maxcount = count;
 16             }   
 17         }   
 18         else
 19         {
 20             count = 0;
 21         }   
 22         
 23         p++;
 24     }   
 25     n = maxcount;
 26     while( n-- )
 27     {
 28         *intput++ = *maxp++;
 29         
 30     }   
 31     return maxcount;
 32 }   
 33 int main()
 34 {
 35     char str[100] = "345as3456ty78";
 36     char intput[100] = {0};
 37     cout <<continueNum(str,intput) << endl;
 38     cout << intput << endl;
 39     return 0;
 40 }   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值