習題11-3 計算最長的字符串長度(15 分)


本題要求實現一個函數,用於計算有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;
} 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM