1170: 最长字符串(指针专题)

1170: 最长字符串(指针专题)

题目描述
输入多个字符串,输出最长字符串。要求定义并使用函数maxLenStr(),
void maxLenStr(char str[], int n, int max)
{
从字符串数组str中找出最长的一个字符串,并将其下标存入形参指针max所指内存。
}
输入
输入有多行,每行一个字符串,每个字符串长度不超过80,输入最多不超过100行,用
**作为结束输入的标志,该行输入不用处理。
输出
输出最长的一个字符串。
样例输入 Copy
L love C programming
ACM/ICPC
study hard


样例输出 Copy
L love C programming
来源/分类

#include<stdio.h>
#include<string.h>
#define N 100

void maxLenStr(char *str[],int n,int *max){
    for(int i=1;i<n;i++){
        if(strlen(str[i])>strlen(str[*max])) *max=i;
    }
}

int main(){
    char *str[101],s[82];
    int i,max=0;
    for(i=0;strcmp(s,"****")!=0;i++){
        gets(s);
        str[i]=(char *)malloc(sizeof(char)*strlen(s));
        strcpy(str[i],s);
    }
    maxLenStr(str,i,&max);
    puts(str[max]);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值