百度校招笔试题

查找得到一个字符串中最长的数字串,并返回该数字串的长度。

编写一段程序实现该函数,实现返回一个以“\\0”结束的字符串中最长的数字串的长度,并把该数字子

串的首地址赋给outputstr。不能使用任何库函数或已经存在的函数,如strlen。

例如:在字符串“abc123abcdef12345abcdefgh123456789”中,把该字符串的首地址赋给inputstr,

返回9,outputstr指向字符串“123456789”的首地址。

int maxContinuNum(const char *inputstr,char *outputstr)
{
//	int len = strlen(inputstr);
	if(inputstr==NULL)
		return NULL;

	int count = 0,max = 0;
	char *tmp_ptr;   //保存数字串首地址
	string tmpstr, outstr;   //tmpstr记录每个数字串,outstr用来保存当前最长数字串
	
	int i = 0;
	while(inputstr[i]!='\0')
	{
		char c = inputstr[i];
		int a = c-'0';
		if(a>=0&&a<=9)
		{
			if(count==0)
				tmpstr = "";
			count++;
			tmpstr += c;
		}
		else
		{
			if(count>max)   //如果当前数字串更长,更新outstr
			{
				outstr = "";
				max = count;
				outstr = tmpstr;
			}
			count = 0;
		}

		i++;
	}
	if(count>max)
		{
			outstr = "";
			max = count;
			outstr = tmpstr;
		}

	//把得到的最长数字串复制给outputstr
	for(int i=0;i<max;i++)
	{
		outputstr[i] = outstr[i];
	}
	outputstr[max] = '\0';

	return max;
}

int main()
{
	char ss[] = "abc123hdfg123456lkjiuyt123456789";
	char outputstr[100] = "";
	int max = maxContinuNum(ss,outputstr);
	cout<<max<<endl;
	int len = strlen(outputstr);
	for(int i=0;i<len;i++)
		cout<<outputstr[i]<<endl;

	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值