- 定义字符串 s,需要被替换的字符 c;
- 遍历字符串 s, 如果 字符与 c 相同, 输出 #, 否则原样输出。
#include <iostream>
using namespace std;
int main()
{
string s;//字符串
cin >> s;
char c;//待替换字符
cin >> c;
for(auto e : s)//范围 for 循环
{
if(c == e) //是待替换的字符
cout <<"#";
else//不是待替换的字符
cout<<e;
}
}
#include[HTML_REMOVED]
using namespace std;
int main(){
string s;char t;
cin>>s;
getchar();
scanf(“%c”,&t);
string new_s;
replace_copy(s.begin(), s.end(), back_inserter(new_s), t, ‘#’);
cout << new_s << endl;
return 0;
}
shaonvkouao