3091 - 2000年秋浙江省计算机等级考试二级C 编程题(2)

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB

编制函数del_char

函数原型为 void del_char(char *,char),函数的功能是删除a指向的字符串中值为ch的字符,例如从字符串"AscADef"中删除'A'后,字符串为"scDef"。

题目输入

需要删除的字符ch

需要处理的字符串

题目输出

处理后的字符串

输入/输出样例

输入格式

A
AscADef

输出格式

scDef

C语言解答

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <malloc.h>

void del_char(char *S,char D)
{
	char a[100];
	char *p;
	while (p=strchr(S,D))
	{
		strcpy(a,p+1);
		strcpy(p,a);
	}
}

int main()
{
	char ch,st[100];
	scanf ("%c",&ch);
	getchar();
	gets(st);
	del_char(st,ch);
	printf ("%s\n",st);
	return 0;
}

C++解答

#include <stdio.h>
#include <string.h>
void del_char(char *s,char c)
{
 int i;
 char temp[256];
    strcpy(temp,s);
 for(i=0;i<strlen(temp);i++)
   {
      if (temp[i]!=c)
        *s++=temp[i];
   }
      
      *s='\0';
}
int main()
{
 char c,s[256];
 scanf("%c%s",&c,s);
 del_char(s,c);
 puts(s);
 return 0;
 
}