蓝桥杯 算法训练 最长字符串(c语言版详细注释)

试题 算法训练 最长字符串

                                                                                  蓝桥杯试题解答汇总链接

资源限制

       时间限制:1.0s 内存限制:512.0MB


问题描述

       求出5个字符串中最长的字符串。每个字符串长度在100以内,且全为小写字母。


样例输入

one two three four five

样例输出

three

代码

#include<stdio.h>
#include<string.h>
int main()
{
	char a[4][101],str[101];//str数组用来保存最长的字符串 
	int i,max;
	scanf("%s",str);//首个字符串复制给str 
	max=strlen(str);//max保存str的长度 
	for(i=0;i<4;i++){
		scanf("%s",&a[i]);
		if(max<strlen(a[i])){//若max小于a[i]的长度则复制给str 
			max=strlen(a[i]);
			strcpy(str,a[i]);
		}
	}
	printf("%s",str);
	return 0;
}
发布了51 篇原创文章 · 获赞 58 · 访问量 4619

猜你喜欢

转载自blog.csdn.net/xyf0209/article/details/104458617