本題要求實現一個函數,用於計算有n個元素的指針數組s中最長的字符串的長度。
函數接口定義:
int max_len( char *s[], int n );
其中n
個字符串存儲在s[]
中,函數max_len
應返回其中最長字符串的長度。
裁判測試程序樣例:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXN 10
#define MAXS 20
int max_len( char *s[], int n );
int main()
{
int i, n;
char *string[MAXN] = {NULL};
scanf("%d", &n);
for(i = 0; i < n; i++) {
string[i] = (char *)malloc(sizeof(char)*MAXS);
scanf("%s", string[i]);
}
printf("%d\n", max_len(string, n));
return 0;
}
/* 你的代碼將被嵌在這里 */
輸入樣例:
4
blue
yellow
red
green
輸出樣例:
6
int max_len( char *s[], int n ) { int m=0; for(int i=0;i<n;i++) { int t=strlen(s[i]); if(m < t) { m=t; } } return m; }