Ubuntu Linux c编程:实现查找字符串中最长数字子串的长度,并将其输出

编程要求:实现查找字符串中最长数字子串的长度,并将其输出(可以改进:用while实现运行效率高)
#include<stdio.h>

#define MAX_SIZE 1024

int main()
{
void search_numstr();
char str1[MAX_SIZE]={0};
printf(“请输入字符串”);
gets(str1);
search_numstr(str1);

}

void search_numstr(char* str)
{
int i;
int j;
int x;
int y;
int count=0,len=0;
char str2[MAX_SIZE]={0};
for(i=0;(str+i) != ‘\0’;i++)
{
for(j=0;
(str+i+j)>=‘0’&&*(str+i+j)<=‘9’;j++)
{
count++;//当前数字子串的长度
}

    if(len<count)//如果找到了更长的数字子串
    {
        len =count;
        for(x=0;x<len;x++)//每次都进行覆盖,留下最后最长的数字子串
        {
            *(str2+x) = *(str+x+i);
        }
        *(str2+len) = '\0'; 
    }
    count = 0;                   
}
printf("%d\n",len);
for(x=0;x<len;x++)
{
    printf("%c",*(str2+x));
}
printf("\n");

}
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值