ZZULIOJ: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 <stdlib.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;
}

猜你喜欢

转载自blog.csdn.net/couchpotatoshy/article/details/126111142