所属年份:2011.9;2012.3;
下列给定程序中,函数fun的功能是:比较两个字符串,将长的字符串的首地址作为函数值返回。
请改正程序中的错误,使它能得出正确的结果。
注意:部分源程序在文件MODI1.C中,不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include <conio.h> #include <stdio.h> /*************found**************/ double fun(char *s,char *t) { int s1=0,t1=0; char *ss,*tt; ss=s; tt=t; while(*ss) { s1++; /*************found**************/ (*ss)++; } while(*tt) { t1++; /*************found**************/ (*tt)++; } if(t1>s1) return t; else return s; } void main() { char a[80],b[80]; printf("\nEnter a string : "); gets(a); printf("\nEnter a string again: "); gets(b); printf("\nThe longer is :\n\n%s\n", fun(a,b)); }
【参考答案】
(1)char *fun( char *s,char *t) (2)ss++; (3)tt++;
【解题思路】
(1)在主函数的输出语句中,函数fun是以字符串格式输出的,所以定义函数时应为char *fun( char *s,char *t)。
(2)和(3)这里是地址加1,而不是内容加1,所以改为ss++和tt++。