去掉字符串中指定字符

题目描述:


输入字符串s和字符c,要求去掉s中所有的c字符,并输出结果。

输入:


测试数据有多组,每组输入字符串s和字符c。

输出:

对于每组输入,输出去除c字符后的结果。

样例输入:

heallo
a

样例输出:


hello

C语言版:

#include <iostream>
#include <stdio.h> 
#include <stdlib.h>
#include <string.h>//字符串问题,加上这个库函数 
using namespace std;


//C版
int main()
{
	char s[1000];
	char c;
	int len;
	while(gets(s)) //gets()从标准输入设备读字符串函数
	{
		len=strlen(s);//字符串的长度,也把结尾符'\0'计算在内了
		scanf("%c",&c);
		for(int i=0;i<len;i++)
		{
			if(s[i]==c)
			{
				continue;
			}	
			else
			{
				printf("%c",s[i]);
			}
		} 
		printf("\n");
		getchar(); //连续输入一个字符或字符串 
	}
	return 0;	
} 

C++版:

#include <iostream>
#include <stdio.h> 
#include <stdlib.h>
#include <string.h>//字符串问题,加上这个库函数 
using namespace std;
//C++版 
int main() {
	char s[1000];
	while((cin>>s)&&s!=NULL)//无限次输入 
	{
		char c;
		cin>>c;
		int i=0,j=0;
		while(s[i]!='\0')//输出字符串,可以用while循环,条件是最后一个字符不为‘\0’ 
		{
			if(s[i]==c)
			{
				i++;
				continue;
			}
			else
			{
				cout<<s[i];
				i++;
			}	
		}	
		cout<<endl;
	}
	return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值