1. 程式人生 > >7-5 找最長的字串 (15 分)

7-5 找最長的字串 (15 分)

本題要求編寫程式,針對輸入的N個字串,輸出其中最長的字串。
輸入格式:

輸入第一行給出正整數N;隨後N行,每行給出一個長度小於80的非空字串,其中不會出現換行符,空格,製表符。
輸出格式:

在一行中用以下格式輸出最長的字串:

The longest is: 最長的字串

如果字串的長度相同,則輸出先輸入的字串。
輸入樣例:

5
li
wang
zhang
jin
xiang

輸出樣例:

The longest is: zhang

#include<stdio.h>
#include<string.h>
#define MAX 100
int main()
{
	int n;
	char s1[MAX],s2[MAX]; 
	scanf("%d",&n);
	gets(s1);
	strcpy(s2,s1);
	for(int i=0;i<n;i++)
	{
		gets(s1);
		if(strlen(s1)>strlen(s2))
			strcpy(s2,s1);
	}
	printf("The longest is: %s",s2);
	return 0;
}