字符串中连续最长的数字字符串

//输入:一个含有数字的字符串;
//输出:字符串中连续最长的数字字符串。
//例如:输入:a12ab123park12345   输出:12345
法一:
#include <stdio.h>

void my_strcpy(char *num,char *p,int len) //字符串复制法一
{
    while(len)
	{
	    len--;
        *num = *p;
		num++;
        p++;
	}
}

/*
void my_strcpy(char *str,char *ptr)      //字符串复制法二
{
    while(*str)
	{
	    *str = *ptr;
		str++;
		ptr++;
	}
	*ptr = '\0';
}
*/

void select(char *arr,char *num)
{
    int count = 0;
	int max = 0;
    while(*arr != '\0')
	{
		while(*arr >= '0' && *arr <= '9')  //当为连续字符时循环
		{
			arr++;
			count++;
		}
        if(count > max)
		{
			max = count;
            my_strcpy(num,arr - count,count);
		}
		count = 0;
		arr++;
	}
}

int main()
{	
	char arr[100] = {0};
    char num[20] = {0};

	printf("Please input a string:");
	scanf("%s",arr);
    select(arr,num); 
	printf("%s\n",num);

	return 0;
}
法二:
#include <stdio.h>
#include <string.h>
/*
int counting(char a[10][10],int z)//计算某行数组的长度
{
   i = z;
	int j = 0;
	int count = 0;
    for(j = 0; a[i][j] != '0'; j++)
	{
	    count++;
	}
	return count;
}
*/
void select(char *arr,char a[10][10])
{
    int i = 0;
	int j = 0;
	int z = 0;
	int max = 0;
	int count = 0;          //数组一行的元素个数
	int max_line = 0;         //行数
	while(*arr != '\0')
	{
	    while(*arr >= '0' && *arr <= '9')
		{
		    a[i][j] = *arr;
			j++;
			arr++;
		}
		i++;
		arr++;
	}
    for(z = 0; z <= i; z++)  //选出最长的一行    
	{
//		count = counting(a,z);
        count = strlen(a[z]);
        if(count > max)
		{
		    max = count;
            max_line = z;
		}
	}
/*	for(j = 0;j < max;j++ )
	{
	    printf("%c",a[max_line][j]);
    }
*/	printf("%s\n",a[max_line]);
}

int main()
{
    char arr[100] = {0};
	char a[10][10] = {0};

	printf("Please input a string:");
	scanf("%s",arr);

	select(arr,a);

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值