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

  • Post author:
  • Post category:其他


//

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

/

#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;

}

}



版权声明:本文为lyc_daniel原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。