问答题
1. 请编写一个函数proc(),它的功能是:将str所指字符串中所有下标为奇数的字母转换为大写(若该位置上不是字母,则不转换)。
例如,若输入“abcde123”,则应输出“aBcDe123”。
注意:部分源程序如下。
请勿改动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\nAfter changing, the string
\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==1&&str[i]>='a'&&str[i]<='z') //ss
所指字符串中所有下标为奇数的字母
str[i]=str[i]-32; //将其转化为大写
}
【答案解析】 要将字符串中为下标为奇数的字母转换为大写,首先应该判断奇数位置上的字符是否为小写字母,若是,则将其转换为大写,若不是,则不予处理。