所属年份:2012.3
给定程序中,函数fun的功能是:判断形参s所指字符串是否是”回文”(Palindrome),若是,函数返回值为1;不是,函数返回值为0。”回文”是正读和反读都一样的字符串(不区分大小写字母)。
例如,LEVEL和Level是”回文”,而LEVLEV不是”回文”。
请在程序的下画线处填入正确的内容并把下画线删除,使程序得出正确的结果。
注意:部分源程序在文件BLANK1.C中。
不得增行或删行,也不得更改程序的结构!
#include <stdio.h> #include <string.h> #include <ctype.h> int fun(char *s) { char *lp,*rp; /**********found**********/ lp= __1__ ; rp=s+strlen(s)-1; while((toupper(*lp)==toupper(*rp)) && (lp<rp) ) { /**********found**********/ lp++; rp __2__ ; } /**********found**********/ if(lp<rp) __3__ ; else return 1; } main() { char s[81]; printf("Enter a string: "); scanf("%s",s); if(fun(s)) printf("\n\"%s\" is a Palindrome.\n\n",s); else printf("\n\"%s\" isn't a Palindrome.\n\n",s); }
【参考答案】
(1)s (2)-- (3)return 0
【解题思路】
填空1:根据函数体fun中,对变量lp和rp的使用可知,lp应指向形参s的起始地址,rp指向s的结尾地址,所以应填s。
填空2:rp是指向字符串的尾指针,当每做一次循环rp向前移动一个位置,所以应填:–。
填空3:当lp和rp相等时,表示字符串是回文并返回1,否则就返回0,所以应填return 0。