第25 题: 写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)

本文介绍了一个用于查找字符串中连续最长数字串的C语言函数。该函数不仅能返回最长数字串的长度,还能将该数字串复制到指定内存区域。文章通过具体示例展示了如何使用此函数。

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

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


这个题目类似与 编写 char* strcpy(char* strdes,const char* strsrc);

主要考虑题目的要求以及一些安全的检验。

#include <assert.h>
#include <stdio.h>

int continuemax(char* output,char *input)
{
	char *temp = input;
	char *maxstr = NULL;
	int maxnum = 0;
	assert( (output != NULL) && (input != NULL) );

	while ( *input != '\0' )
	{
		while ( *input > '9' || *input < '0' )
			input ++;

		temp = input;

		while ( *input >= '0' && *input <= '9' )
			input ++;

		if (input - temp > maxnum)
		{
			maxstr = temp;
			maxnum = input - temp;
		}
	}

	while ( *maxstr >= '0' && *maxstr <= '9' )
		(*output ++) = (*maxstr ++);
	*output = '\0';

	return maxnum;
}

void main()
{
	char *input = "1244121hdkahdh327348y1284y8ahd";
	char output[20];

	printf("%d\n",continuemax(output,input));

	printf("%s\n",output);
	return 0;
}




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值