填空题
1. 从键盘输入一组小写字母,保存在字符数组str中。请补充函数proc(),该函数的功能是:把字符数组str中字符下标为奇数的小写字母转换成对应的大写字母,结果仍保存在原数组中。
例如,输入“abcdefg”,输出“aBcDeFg”。
注意:部分源程序如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的横线上填入所编写的若干表达式或语句。
试题程序:
#include<stdlib.h>
#include<stdio.h>
#define M 80
void proc(char str[])
{
int i=0;
while(______)
{
if(i%2!=0)
str[i]-=______;
______;
}
}
void main()
{
char str[M];
system("CLS");
printf("\n Input a string:\n");
gets(str);
printf("\n***original string***\n");
puts(str);
proc(str);
printf("\n***new string***\n");
puts(str);
}
【正确答案】
1、str[i]!='\0'
32
i++
【答案解析】 要将字符串中所有下标为奇数的小写字母转化为大写字母,应该检查字符串str中第一个到最后一个字符,判断其下标是否为奇数,因此,第1空处填“str[i]!='\0'”。每找到一个下标为奇数的小写字母,就将其转换为大写字母,大写字母的ASCII码值比与其对应的小写字母小32,因此,第2空处填“32”。每判断完一个字符,要为检查下一个字符做准备,因此,第3空处填“i++”。