C++字符串字符替换程序
这是一段用于字符串替换的C++代码。程序接收一个字符串和两个字符作为输入,将字符串中所有与第一个字符匹配的字符替换为第二个字符,然后输出修改后的结果。例如,输入"hello"、'l'和'x',输出"hexxo"。代码使用简单的循环遍历字符串进行逐个字符比较和替换,最后输出处理后的字符串。
输入
Cpp
p +
输出
C++。
#include<bits/stdc++.h>
using namespace std;
int main(){string str;cin>>str;char c1,c2;cin>>c1>>c2;for(int i=0;i<str.length();i++){if(str[i]==c1){cout<<c2;}else{cout<<str[i];}}return 0;
}