6-2 计算最长的字符串长度 (10 分)

6-2 计算最长的字符串长度 (10 分)

本题要求实现一个函数,用于计算有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
作者: C课程组
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB
编译器:共 1 种编译器可用

1
2
3
4
5
6
7
8
9
10
11
12

\\

fa 1:
int max_len( char *s[], int n )
{
int i,k=-1;
for(i=0;i<n;i++){
int len=strlen(s[i]);
if(k<len){
k=len;
}
}
return k;
}

提交返回

fa 2:
int max_len( char *s[], int n ){
int i;
int max=0;
for(i=0;i<n;i++){
if(max <strlen(s[i]))
max = strlen(s[i]);
}
return max;

}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值