Write a C++ program that accepts a string from the user and then replaces all occurrences of the letter e with the letter x.
时间: 2024-05-10 10:19:07 浏览: 132
C++ 字符替换
Here's a sample C++ program that replaces all occurrences of the letter "e" with "x" in a user-input string:
```cpp
#include <iostream>
#include <string>
int main() {
std::string input;
std::cout << "Enter a string: ";
std::getline(std::cin, input);
for (auto& c : input) {
if (c == 'e') {
c = 'x';
}
}
std::cout << "Modified string: " << input << std::endl;
return 0;
}
```
In this program, we first ask the user to input a string using `std::getline()`. We then loop through each character in the string, and if we encounter the letter "e", we replace it with "x". Finally, we output the modified string using `std::cout`.
阅读全文