C语言编程>第二十一周 ① 下列给定程序中,函数fun的功能是:比较两个字符串,将长的字符串的首地址作为函数值返回。

例题:下列给定程序中,函数fun的功能是:比较两个字符串,将长的字符串的首地址作为函数值返回。

例如,输入 两串字符分别为“asdf”和"asdfg",则输出为 “asdfg”。
注意:不要改动main函数,不能增行或删行,也不能更改程序的结构。

代码如下:

#include<conio.h>
#include<stdio.h>
char*fun(char*s,char*t)
{
    
    
	int s1=0,t1=0;
	char*str1,*str2;
	str1=s;
	str2=t;
	while(*str1)
	{
    
    
		s1++;
		str1++;
	}
	while(*str2)
	{
    
    
		t1++;
		str2++;
	}
	if(t1>s1)
		return t;
	else
		return s;
}
main()
{
    
    
	char p[80],q[80];
	printf("\nEnter a string: ");
	gets(p);
	printf("\nEnter another string:");
	gets(q);
	printf("\nThe longer is:%s\n",fun(p,q));
}

输出运行窗口如下:
在这里插入图片描述

越努力越幸运!
加油,奥力给!!!

猜你喜欢

转载自blog.csdn.net/qq_45385706/article/details/112800051