输入字符串s和字符c,要求去掉s中所有的c字符,并输出结果。
输入:测试数据有多组,每组输入字符串s和字符c。
输出:对于每组输入,输出去除c字符后的结果。
样例输入:heallo a样例输出:
hello
第一种方法:
?
#include#include using namespace std; int main() { string s; //×?·??? while (cin>>s) { char c; cin>>c; for (int i = 0; i < s.length(); i++) { if (s[i] != c) { cout< 第二种方法:?
?
/* 这种思路: 比较两个字符串数组,如果相同的则用一个特定的字符去替换。由于是字符串,比较特殊,所以选择用'\n'去替换! 有个疑问的就是为什么再输入的时候用getline,第一次显示的结果是对的,当进行第二轮测试的时候就不对了!如 果有兴趣的可以自己试试。 */ #include#include using namespace std; int main() { string str1,str2; int i,j; //while(cin,str1) while (cin>>str1) { int x=i; //getline(cin,str2); //不知道为什么,这里和while循环里用getline时,第一次比较显示是对的,第二次就不对了! //如果有大牛知道知道,还望告知一下! cin>>str2; for (int i=0;i ?