1. 程式人生 > >PTA-找最長的字串(C語言)

PTA-找最長的字串(C語言)

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

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

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

The longest is: 最長的字串
如果字串的長度相同,則輸出先輸入的字串。

輸入樣例:
5
li
wang
zhang
jin
xiang

輸出樣例:
The longest is: zhang

#include <stdio.h>
#include<string.h>
int main(){
	int N,n,max=0,t=0;  
	scanf("%d",&N);
    char num[N][100];
    int num1[N];  //儲存各個字串的長度 
    for(int i=0;i<N;i++){
    	scanf("%s",num[i]);
    	num1[i]=strlen(num[i]);
    	if(max<num1[i]){
    		max=num1[i];
    		t=i;
		} 
	}
    printf("The longest is: %s",num[t]);
	return 0;
}