题目描述:
-
输入字符串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;
}