試題 算法訓練 最長字符串
資源限制
時間限制: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;
}