c 语言 1176 最大字符串 指针专题

/*****
题目描述
从键盘上输入多个字符串(每个串不超过5个字符且没有空格),用”*****”作为串输入结束的标记。从所输入的若干字符串中,找出一个最大的串,并输出该串。要求最大串的查找通过调用编写的函数实现
void find(char *name[], int n, int *p)
{
//在包含n个字符串的二维字符数组name中,查找值最大的字符串,将其下标存入指针p所指内存单元
}

输入
一行输入一个字符串,输入多行

输出
输出一个字符串,单独占一行。

样例输入 Copy
zzzdf
fdsg
adff
rtrt


样例输出 Copy
zzzdf
*****/
本题和1170题非常类似,从那里修改一些代码就行了。 1170 是输出最长字符串,这个是输出最大的字符串。

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

void find(char *name[], int n, int *p);

int main()
{
    char *str[N];
    char a[20];
    int *max;
    int i;
    max = (int *)malloc(sizeof(int));
    //Íê³ÉÊäÈë
    for(i = 0;; i++)
    {
        gets(a);
        if(strcmp(a,"*****") == 0)
            break;
        else
        {
            str[i] = (char*)malloc(sizeof(char)*(strlen(a)+1));
            strcpy(str[i],a);
        }
    }
    find(str,i,max);
    printf("%s",str[*max]);
    return 0;
}
void find(char *name[], int n, int *p)
{
    int i;
    char maxx[5];
    memset(maxx,'\0',sizeof(maxx));
    for(i = 0; i<n; i++)
    {
        if(strcmp(name[i],maxx) >= 0)
        {
            strcpy(maxx,name[i]);
            *p = i;
        }
    }
}

发布了84 篇原创文章 · 获赞 0 · 访问量 1791

猜你喜欢

转载自blog.csdn.net/qq_39345244/article/details/105270979