简答题 C语言
题号 #2255571

请编写函数void proc(char a[],Charb[],int n),其功能是:删除一个字符串中指定下标的字符。其中,a指向原字符串,删除后的字符串存放在b所指的数组中,n中存放指定的下标。
例如,输入一个字符串thanyou,然后输入4,则调用该函数后的结果为thanyou。
注意:部分源程序给出如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数fun()的花括号中填入所编写的若干语句。
试题程序:
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#define N 20
void proc(char a[],char b[],int n)
{

}
void main()
{
char str1[N],str2[N];
int n;
system("CLS");
printf("Enter the string\n");
gets(str1);
printf("Enter the position of the string deleted:
");
scanf("%d",&n);
proc(str1,str2,n);
printf("The new string is:%s\n",str2);
}

进入在线模考

参考答案

暂无

答案解析

void proc(char a[],char b[],int n)
{
int i,k=0:
for(i=0;a[i]!='\0';i++)//要删除,也就是把
下标不是n的字符放到b中
if(i!=n)//n中存放指定的下标
b[k++]=a[i]; //删除指定字符后的字符串存
放在b所指的数组中
b[k]='\0';//用'\0'作为字符串的结束标志
}
【解析】按照题目中的要求,删除一个字符串中指定下标的字符。首先,将指定下标之前的字符放入字符数组b 中,然后将指定下标之后的字符也放入字符数组b中,最后为新的字符数组b添加结束符。

你可能感兴趣的试题

找试题 · 找答案 · 找资料 在线模考