首页 > 学院 > 开发设计 > 正文

例题:找出字符串中最长数字串

2019-11-11 02:20:04
字体:
来源:转载
供稿:网友

题目:输入一个字符串,找出字符串中最长数字

#include<stdio.h>char num_long(char *str); int main(void){	char str[100];	PRintf("Please input a string with numbers:/n");	scanf("%[^/n]",str);    num_long(str);                 //调用查找最长数字串函数}char num_long(char *str){	int loc = 0,max_loc = 0;	int len = 0,max_len = 0;	int flag = 0;	int i = 0;	while(str[i] != '/0')                   	{		if(str[i] >= '0' && str[i] <= '9')		{			if(flag == 0)         //标志是否是数字			{				flag = 1;				loc = i;          //记录数字串首地址			}			len++;                 //记录字符串长度		}		else		{			if(flag == 1)			{				flag = 0;				if(max_len < len)        //比较出最长字符串				{					max_len = len;					max_loc = loc;				}				len = 0;               //置0,以便记录下一个数字串长度			}		}		i++;	}	for(i = max_loc; i < (max_loc + max_len); i++)	{		printf("%c",str[i]);	}}串


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表