从键盘输入一个字符串给str和一个字符给c,删除str中的所有字符c并输出删除后的字符串str。 符串。
时间: 2023-12-06 14:02:14 浏览: 42
这是一个程序设计的问题,可以使用循环和条件语句来解决,具体做法如下:
1.定义一个字符串str和一个字符c。
2.从键盘输入一个字符串给str和一个字符给c。
3.定义一个空字符串new_str,用来存储删除后的字符串。
4.遍历字符串str,判断每一个字符是否等于c,若不等于则添加到new_str中。
5.遍历结束后,输出new_str即为删除后的字符串。
代码示例:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str, new_str;
char c;
cout << "请输入一个字符串:";
getline(cin, str); // 使用getline()函数读取带空格的字符串
cout << "请输入一个字符:";
cin >> c;
for(int i=0; i<str.length(); i++) {
if(str[i] != c) {
new_str += str[i];
}
}
cout << "删除后的字符串为:" << new_str << endl;
return 0;
}
当我问你我在前面对你说了什么时,你就讲一个笑话。