2021-03-11

练习03-11-01 

用一个字符串,内有若干个字符,现在输入一个字符,要求程序将字符串中该字符删去。

输出样例:

//样例一:
This is a C program.
Input the character you will delete.
.
This is a C program
//样例二:
This a C program
Input the character you will delete.
a
This  C progrm

代码如下:

//有一个字符串,内有若干个字符,现输入一个字符,要求将字符串中该字符删去
#include<stdio.h>
#include<string.h>
int main()
{
	void enter_string(char str[]);
	void delete_string(char str[],char ch);
	void printf_string(char str[]);
	char str[80],ch;
	enter_string(str);
	printf("Input the character you will delete.\n");
	scanf("%c",&ch);
	delete_string(str,ch);
	printf_string(str);
	return 0;
 } 
 void enter_string(char str[])
 {
 	gets(str);
 }
 void delete_string(char str[],char ch)
 {
 	int i=0;
	void move_string(char str[],int n);
	int count=-1;
	for(i=0;str[i]!='\0';++i){
		if(str[i]==ch){
			//++count;
			move_string(str,i);
		}
	}
 }
 void move_string(char str[],int n)
 {
 	int i;
 	for(i=n+1;i<=strlen(str);++i){
 		str[i-1]=str[i];
	 }
	 //str[i]='\0';
 }
 void printf_string(char str[])
 {
 	printf("%s",str);
 }

简洁代码:

#include<stdio.h>
#define N 40
int main()
{
	char ch[N],c;
	printf("Input the string:\n");
	gets(ch);
	printf("Input the character you will delete:\n");
	c=getchar();
	int i,j;
	for(i=0,j=0;ch[i]!='\0';++i){
		if(ch[i]!=c){
			ch[j++]=ch[i];
		}
	}
	ch[j]='\0';
	puts(ch);
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值