现在的位置: 首页 > 综合 > 正文

求三个字符串的长度,并输出最长值和最短值及其长度

2013年10月22日 ⁄ 综合 ⁄ 共 1375字 ⁄ 字号 评论关闭

//////////////////////////////////////////////////////
//求三个字符串的长度,并输出最长值和最短值及其长度 
///////////////////////////////////////////////////// 
#include <stdio.h> 
#include <stdlib.h>
#include <string.h> 
char* strLenCmp(char* x,char* y,int* Len,int flag); 
main()
{
      char *str1="I am LYC." ;
      char *str2="My english name is Daniel."; 
      char *str3="Could I make friend with you?";
      char *strSht=NULL;
      char *strLng=NULL; 
      int shtLen=0,lngLen=0;
      printf("The three strings are:\n%s\n%s\n%s\n",str1,str2,str3); 
      
      strSht=strLenCmp(strLenCmp(str1,str2,&shtLen,0),str3,&shtLen,0); //求最短者 
      strLng=strLenCmp(strLenCmp(str1,str2,&lngLen,1),str3,&lngLen,1); //求最长者 
      printf("The shortest string and its length are:\n%s\n%d\n",strSht,shtLen); 
      printf("The longest string and its length are:\n%s\n%d\n",strLng,lngLen); 
      
      system("pause");  

char* strLenCmp(char* x,char* y,int* Len,int flag)
{
      int xLen,yLen; 
      xLen=strlen(x); 
      yLen=strlen(y);
      char *temp=NULL; 
      switch(flag) 
      { case 0:
               {
                  if(xLen<yLen)
                  {
                     *Len=xLen;
                      temp=x; 
                  }
                  else
                  {
                     *Len=yLen;
                      temp=y;   
                  }
                  return temp; 
               }
          case 1:   
               {
               if(xLen<yLen)
                  {
                     *Len=yLen;
                      temp=y; 
                  }
                  else
                  {
                     *Len=xLen;
                      temp=x;   
                  }
                  return temp;              
               } 
          default: break;  
      } 
}

抱歉!评论已关闭.