问答题 请编写一个函数void proc(char *str),其功能是:将字符串str中所有下标为偶数位置上的字母转化为大写(若该位置上不是字母,则不转换)。
例如,若输入字符串:“thank you6 very much”,则应输出:“ThAnK YoU6 vErY MuCh”。
注意:部分源程序给出如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的花括号中填入所编写的若干语句。
试题程序:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void proc(char *str)
{

}
void main()
{
char tt[51];
system("CLS");
printf("Please enter an character string within 50 characters: /n");
gets(tt);
printf("/n/nAfter changing, the string/n%s", tt);
proc(tt);
printf("/nbecomes/n%s", tt);
}
【正确答案】
【答案解析】void proc(char*str)
{ int i;
for(i=0; str[i]!="/0"; i++)
{
if(i%2==0&&str[i]>="a"&&str[i]<="z") //字符串ss中所有下标为偶数位置上的字母
str[i]=str[i]-32;} //将其转化为大写
} [解析] 要将字符串str中下标为偶数位置上的字符转化为大写字母,首先应该判断字符串str中下标为偶数位置上的字符是否为小写字母,若是将其转化为大写字母,若不是不做任何变化。