2015年计算机二级C语言上机操作题及答案(31)
发布时间:2011/7/10 10:22:41 来源:城市学习网 编辑:ziteng
第31套
填空题
请补充fun函数,该函数的功能是:把主函数中输入的字符串str2接在字符串str1后面。
例如:str1=”How are”,str=”you?”,结果输出:How are you?
注意:部分源程序给出如下
请勿改动主函数main和其他函数中的任何内容,仅在fun函数的横线上填入所编写的若干表达式或语句。
试题程序:#include <stdio.h>
#include <conio.h>
#define N 40
void fun(char *str1, char *str2)
{
int i = 0;
char *p1 = str1;
char *p2 = str2;
while (___1___)
i++;
for (; ___2___; i++)
*(p1+i) = ___3___;
*(p1+i) = '\0';
}
main()
{
char str1[N], str2[N];
int m, n, k;
printf("******* Input the string str1 & str2*******\n ");
printf(" \nstr1:");
gets(str1);
printf(" \nstr2:");
gets(str2);
printf("******* The string str1 & str2*******\n");
puts(str1);
puts(str2);
fun(str1, str2);
printf("******* The new string *******\n");
puts(str1);
}
第1处填空:*(pl+i)或pl[i]或*(pl+i)!=0或pl[i]!=0
第2处填空:*p2或p2[0]或*p2!=0或p2[0]!=0
第3处填空:*p2++ [NextPage] 改错题
下列给定程序中,函数fun的功能是:将字符串tt中的小写字母都改成对应的大写字母,其他字符不变,例如输入Ab,cD,则输出AB,CD。
请改正程序中的错误,使其能得出正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题 程序:
#include <conio.h>
#include <stdio.h>
#include <string.h>
char *fun(char tt[])
{
int i;
/********found********/
for (i=0; tt[i]; i++)
if ((tt[i] >= 'a') || (tt[i]<='z'))
/********found********/
tt[i] += 32;
return (tt);
}
main()
{
char tt[81];
printf("\nPlease enter a string:");
gets(tt);
printf("\nThe result string is :\n%s", fun(tt));
}
第1处:if ((tt[i]>=’a’)||(tt[i]<=’z’))应改为if ((tt[i]>=’a’)&&(tt[i]<=’z’))
第2处:tt[i]+=32;应改为tt[i]-=32; [NextPage] 编程题
请编写函数FUN,其功能是:将S所指字符串中除了下标为偶数、同时ASCII值也为偶数的字符外,其余 的全都删除:串中剩余字符所形式的一个新串放在T所指的数组中。
例如,若S所指字符串中的内容为ABCDEFG123456,其中字符A的ASCII码值为奇数,因此应当删除:其中字符B的ASCII码值为偶数,但在数组中的下标为奇数:因此也应当删除:而字符2的ASCII码值为偶数,所在数组中的下标也为偶数,因此不应当删除,其他依此类推。最后T所指的数组中的内容应是246。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:#include <conio.h>
#include <stdio.h>
#include <string.h>
void fun(char *s, char t[])
{
}
main()
{
char s[100], t[100];
FILE *out;
printf("\nPlease enter string S:");
scanf("%s", s);
fun(s, t);
printf("\nThe result is : %s\n", t);
out=fopen("out.dat", "w");
strcpy(s, "Please enter string S:");
fun(s, t);
fprintf(out, "%s", t);
fclose(out);
}
答案是:
void fun(char *s,char t[])
{
int I,j=0,n;
n=strlen(s);
for(i=0;i<n;i++)
if(i%2==0&&s[i]%2==0)
{
t[j]=s[i];
j++;
}
t[j]=’\0’;
}