P084 找出最长的一个字符串 ★★

03-程序设计题 软件121, 唐鼎威 1063浏览

所属年份:2010.9;2011.3;2011.9;
编写一个函数,其功能是:从传入的num个字符中找出最长的一个字符串,并通过形参指针max传回该串地址(用****作为结束输入的标识)。

#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char *fun(char (*a)[81], int num, char *max)
{
  
}
void main()
{
  FILE *wf;
  char ss[10][81],*ps=NULL;
  char s[3][81]={"abcd","deg","diegns"},*p=NULL;
  int  i=0,n;
  system("CLS");
  printf("输入若干个字符串:");
  gets(ss[i]);
  puts(ss[i]);
  while(!strcmp(ss[i], "****")==0)  /*用4个星号作为结束输入的标志*/
     {
      i++;
      gets(ss[i]);
      puts(ss[i]);
     }
  n=i;
  ps=fun(ss,n,ps);
  printf("\nmax=%s\n",ps);
/******************************/
  wf=fopen("out.dat","w");
  p=fun(s,3,p);
  fprintf(wf,"%s",p);
  fclose(wf);
/*****************************/
}

【解题思路】
解答本题之前,首先应该明白ss是一个指向一维数组的指针变量,max是指向指针的变量,所以引用变量时要注意加上*。本程序使用循环语句遍历字符串数组,使用条件语句判断该字符串是否最大。
【参考答案】

char *fun(char (*a)[81], int num, char *max)
{
   int i=0;
   max=a[0];
   for(i=0;i<num;i++)         /*找出最长的一个字符串*/
      if(strlen(max)<strlen(a[i])) 
         max=a[i];
   return max;                /*传回最长字符串的地址*/
}