C语言程序练习——(写一个函数,它的原形是int continumax(char *outputstr,char *intputstr))
写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)。
功能:
在字符串中找出连续最长的数字串,并把这个串的长度返回,
并把这个最长数字串付给其中一个函数参数outputstr所指内存。
例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回9,
outputstr所指的值为123456789
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int continumax(char *outputstr, char *intputstr)
{
int max_num_len = 0;
int flag = 0;
int i = 0;
int count = 0;
int area_i = 0;
int area_end_i = 0;
char *tmp = intputstr;
char s1[1024];
while(tmp[i] != '\0')
{
if(tmp[i] > 47 && tmp[i] < 58)
{
if(flag == 0)
{
area_i = i;
flag = 1;
count++;
}
else if(flag == 1)
{
count++;
}
++i;
}
else
{
if(flag == 1)
{
if(count > max_num_len)
{
area_end_i = i;
max_num_len = count;
int j;
for(j = area_i;j < area_end_i;j++)
{
outputstr[j - area_i] = intputstr[j];
}
}
count = 0;
flag = 0;
}
++i;
}
}
if(flag == 1)
{
if(count > max_num_len)
{
area_end_i = i;
max_num_len = count;
int j;
for(j = area_i;j < area_end_i;j++)
{
outputstr[j - area_i] = intputstr[j];
}
}
count = 0;
flag = 0;
}
return max_num_len;
}
int main ()
{
char *str = "abd1234xsac2231231xasc111";
char *outputstr = (char *)malloc(1024);
int intputstr = continumax(outputstr, str);
printf("intputstr = %d\n", intputstr);
printf("outputstr = %s\n", outputstr);
return 0;
}
/*
continumax(outputstr, str);
这个,只是传入了指针 ,用
continumax(char *outputstr, char *intputstr)
接之后,会退化成指针变量,只能修改malloc里面的值,而不能修改 outputstr指向的位置
continumax(&outputstr, str);
这个传入指针地址,用
continumax(char **outputstr, char *intputstr)
用二级指针接之后,能修改output指向的位置
*/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)