2015年计算机二级C语言上机操作题及答案(64)
发布时间:2011/7/20 13:12:09 来源:城市学习网 编辑:ziteng
填空题
请补充main函数,该函数的功能是:把字符串str1中的非空格字符拷贝到字符串str2中.
例如,若str1=”nice to meet you!”,则str2=”nicetomeetyou!”
仅在横线上填入所编写的若干表达式或语句,勿改动函数中的其他任何内容.
#include
#define N 80
main()
{
static char str1[N] = "nice to meet you!";
char str2[N];
int i = 0, j = 0;
printf("\n****** str1 ******\n ");
puts(str1);
while (str1[i])
{
if (___1___)
str2[j++] = str1[i];
___2___;
}
printf("\n****** str2 ******\n ");
for (i=0; i printf("%c", str2[i]);
}
答案:
第1处填空str1[i]!=’ ’或’ ’!= str1[i]
第2处填空i++或++i或i+=1或i=i+1 [NextPage] 改错题
下列给定程序中,函数fun的功能是:将大写字母转换为对应小写字母之后的第五个字母;若小写字母为v~z,使小写字母的值减21.转换后的小写字母作为函数值返回.例如,若形参是字母A,则转换字母为小写字母f;若形参是字母W,则转换为小写字母b.
请改正函数fun中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include
#include
char fun(char c)
{
/********found********/
if (c>='A' && c<='Z')
c = c-32;
/********found********/
if (c>='a' && c<='u')
c = c-5;
else if (c>='v' && c<='z')
c = c-21;
return c;
}
main()
{
char c1, c2;
printf("\nEnter a letter(A-Z): ");
c1 = getchar();
if (isupper(c1))
{
c2 = fun(c1);
printf("\n\nThe letter %c change to %c\n", c1, c2);
}
else
{
printf("\nEnter (A-Z)!\n");
}
}
答案:
第1处:c=c-32;应改为c=c+32;
第2处:c=c-5; 应改为 c=c+5; [NextPage] 编程题
请编写函数fun,其功能是:将s所指字符串中ASCII值为奇数的字符删除,串中剩余字符形成一个新串放在t所指的数组中。
例如,若s所指字符串中的内容为ABCDEFG12345,其中字符A的ASCII码值为奇数、……、
字符1的ASCII码值也为奇数、……都应当删除,其他依次类推。最后t所指的叔祖中的内容应是BDF24。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
#include
#include
void fun( char *s, char t[])
{
}
main()
{
char s[100], t[100], Msg[] = "Please enter string S:";
FILE *out ;
printf(Msg);
scanf("%s", s);
fun(s, t);
printf("\nThe result is :%s\n", t);
out=fopen ("out.dat","w");
fun(Msg, t);
fprintf(out, "%s", t);
fclose (out );
}
答案:
void fun (char*s,char t[])
{
int I, j=0, n;
n=strlen(s);
for(i=0;iif(s[i]%2= =0)
{
t[j]=s[i];
j++;
}
t[j]=’\0’
}