问答题
1. 请编写一个函数proc(),它的功能是:将str所指字符串中所有下标为奇数的字母转换为大写(若该位置上不是字母,则不转换)。
例如,若输入“ab7g88BJ”,则应输出“aB7G88BJ”。
注意:部分源程序给出如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的花括号中填入所编写的若干语句。
试题程序: #include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
void proc(char *str)
{
}
void main()
{
char tt[81];
system("CLS");
printf("<nPlease enter an string within 80
characters:<n");
gets(tt);
printf("<n
%s",tt);
proc(tt);
printf("<nbecomes<n%s<n",tt);
}
【正确答案】void proc(char*str)
{
int i;
for(i=0;str[i]!='<0';i++)
if(i%2!=0&&str[i]>='a'&&str[i]<='z') //找出下标为奇数且为小写字母的元素
str[i]=str[i]-32;//转化为大写
}
【答案解析】 题目要求把下标为奇数的小写字母转化为大写字母,这需要检查字符串中下标为奇数的字符是否为小写字母。若是小写字母则将其转换为大写字母,若不是则不作任何变化。大写字母与小写字母的关系为ASCII码值相差32。