【MAC 上學(xué)習(xí) C++】Day 24-1.習(xí)題11-3 計(jì)算最長(zhǎng)的字符串長(zhǎng)度 (15 分)

習(xí)題11-3 計(jì)算最長(zhǎng)的字符串長(zhǎng)度 (15 分)

1. 題目摘自

https://pintia.cn/problem-sets/12/problems/361

2. 題目?jī)?nèi)容

本題要求實(shí)現(xiàn)一個(gè)函數(shù),用于計(jì)算有n個(gè)元素的指針數(shù)組s中最長(zhǎng)的字符串的長(zhǎng)度。

函數(shù)接口定義:

int max_len( char *s[], int n );
其中n個(gè)字符串存儲(chǔ)在s[]中,函數(shù)max_len應(yīng)返回其中最長(zhǎng)字符串的長(zhǎng)度。

輸入樣例:

4
blue
yellow
red
green

輸出樣例:

6

3. 源碼參考
#include <iostream>
#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;
}

int max_len(char *s[], int n)
{
    int len;
    int i;

    char **p;

    p = s;

    len = strlen(*p);

    for (i = 1; i < n; i++)
    {
        if (len < strlen(*(p + i)))
        {
            len = strlen(*(p + i));
            break;
        }
    }

    return len;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容