填空题 下列给定的程序中,fun函数的功能是:将p所指的字符串中每个单词的最后一个字母改成大写(这里的“单词”是指有空格隔开的字符串)。
例如,若输入:“I am a student to take the examination”,
则应输出:“I aM A studenT tO takE thE examinatioN”。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
void fun(char*p)
{
int k=0;
for(;*p;p++)
if(k)
{
/**********found**********/
if(p=="")
{
k=0;
/**********found**********/
*p=toupper(*(p-1));
}
}
else
k=1;
}
void main()
{
char chrstr[64];
int d;
system("CLS");
printf("/nPlease enter an English sentence within 63 letters:");
gets(chrstr);
d=strlen(chrstr);
chrstr[d]="";
chrstr[d+1]=0;
printf("/nBefore changing:/n%s",chrstr);
fun(chrstr);
printf("/nAfter changing:/n%s",chrstr);
}
【正确答案】
【答案解析】(1)if(*p==" ")
(2)*(p-1)=toupper(*(p-1)); [解析] (1)本题重点考查考生对指针的理解,当引用指针指向的元素时,应使用指针运算符*号。
(2)当p指向空格时,将前面的字符转换为大写,因此此处应为*(p-1)而不是*p。