简答题 C语言
题号 #2253892

编写一个函数,从传人的M个字符中找出最长的一个字符串,并通过形参指针max传回该串地址(用****作为结束输入的标志)。

注意:部分源程序给出如下。

请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的花括号中填入所编写的若干语句。

试题程序:

#include<stdi0.h>

#include<strin9.h>

#include<coni0.h>

char*proc(char(*a)[81],int num)

{

 

}

void main()

{

   char ss[10][81],*max;

   int n,i=0;

   printf("输入若干个字符串:");

   gets(ss[i]);

   puts(ss[i]);

   while(!strcmp(ss[i],"****")==0)

   {

   i+-;

   gets(ss[i]);

   puts(ss[i]);

   }

   n=i:

   max==proc(ss,n);

   printf("\nmax=%s\n",max);

}

进入在线模考

参考答案

暂无

答案解析

char*proc(char(* a)[81],int M)

{

char*max;

int i=0:

max=a[0];

for(i=0;i<M;i++)

if(strlen(max)<strlen(a[i])) //找出最长的字

符串

max=a[i];

return max; //返回最长字符串的地址

}

【解析】本题首先要定义一个字符指针用于保存最长的字符串,并使其初始值指向第一个字符串;再循环遍历字符串数组,通过if语句比较字符串的长度,并把最长的字符串地址赋给字符指针;最后返回最长字符串的地址。

你可能感兴趣的试题

找试题 · 找答案 · 找资料 在线模考