填空题
1. 下列给定程序中,函数fun的功能是:将s所指字符串中的字母转换为按字母序列的后续字母(如“Z”转化为“A”,“z”转化为“a”),其他字符不变。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
void fun(char *s)
{
/*********found*********/
while(*s!='@')
{
if(*s>='A'&&*s<='Z'
||*s>='a'&&*s<='z')
{
if(*s=='Z') *s='A';
else if(*s='z') *s='a';
else *s+=1;
}
/*********found*********/
(*s)++;
}
}
void main()
{
char s[80];
system("CLS");
printf("\n Enter a string with length<80:\n\n");
gets (s);
printf("\n The string:\n\n");
puts(s);
fun(s);
printf("\n\n The Cords:\n\n");
puts(s);
}
【正确答案】
1、(1)while(*s)或while(*s! ='\0')
(2)s++;
【答案解析】
(1)通过while语句可对字符串所有字符进行遍历,循环条件是对当前字符进行判断,若当前字符不是字符串结尾,则对其进行其他操作。
(2)因为该循环通过指针s的移动遍历字符串,所以每循环一次要使指针向后移动一个位置,而不是将指针所指的元素加1。