为了帮助大家复习好计算机等级考试,本次我们考吧网特为广大考生整理了计算机等级考试模拟试题,希望广大考生能够喜欢。本次主要汇编了计算机等级二级C语言精选试题:
三、编程题
请编写一个函数void fun (char a[],char b[],int n),其功能是:删除一个字符串中指定下标的字符。其中, a指向原字符串, 删除指定字符后的字符串存放在b所指的数组中,n中存放指定的下标。
例如,输入一个字符串: World,然后输入3,则调用该函数后的结果为: Word。
注意: 部分源程序在文件PROG1.C中。
请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
给定源程序:
#include
#include
#define LEN 20
void fun (char a[], char b[], int n)
{
}
main( )
{ char str1[LEN], str2[LEN] ;
int n ;
printf("Enter the string:") ;
gets(str1) ;
printf("Enter the position of the string deleted:") ;
scanf("%d", &n) ;
fun(str1, str2, n) ;
printf("The new string is: %s", str2) ;
NONO() ;
}
解题思路:
本题是利用字符串拷贝和字符串连接来生成新的字符串。
参考答案:
void fun (char a[], char b[], int n)
{
strncpy(b, a, n) ;
b[n] = 0 ;
strcat(b, a + n + 1) ;
}
编辑推荐: