【无标题】

该程序定义了一个函数`max_len`,其目的是在给定的包含n个字符串的指针数组`s`中找到最长的字符串并返回其长度。在主函数中,读取n个字符串并分配内存,然后调用`max_len`函数输出最长字符串的长度。在`max_len`函数中,通过遍历数组并使用`strlen`函数比较字符串长度来找到最长的字符串。
摘要由CSDN通过智能技术生成

6-11 计算最长的字符串长度

分数 10

全屏浏览题目

切换布局

作者 C课程组

单位 浙江大学

本题要求实现一个函数,用于计算有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 i;
int max=strlen(s[i]);//一定是max=strlen(s[i]);不是max=s[0];
   for(i=0;i<n;i++)
   {
    if(strlen(s[i])>=max)
    max=strlen(s[i]);
   }
return max;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值