例如:
输入“They are students.” 和 “aeiou”,则删除之后的第一个字符串变成“Thy r stdnts.”
输入描述: 每个测试输入包含2个字符串。
输出描述: 输出删除后的字符串 。
示例:输入They are students. 和 aeiou 输出 Thy r stdnts.
#include <iostream>
#include <string>
using namespace std;
string string_delete(string & str, int i){
for (int j = i + 1; j < str.size() + 1; j++){
str[i] = str[j];
i++;
}
return str;
}
int main(){
string str1;
string str2;
int i, j;
//不能用cin接受字符串,因为cin遇到空格就结束了
//输入字符串最好用getline
getline(cin, str1);
getline(cin, str2);
for (i = 0; i < str1.size(); i++){
for (j = 0; j < str2.size(); j++){
if (str1[i] == str2[j]){
string_delete(str1, i);
}
}
}
cout << str1;
system("pause");
return 0;
}
注意事项:
1、不能用cin接受字符串,因为cin遇到空格就结束了,输入字符串最好用getline。
2、我才用的算法比较粗暴,将两个字符串遍历一遍,进行寻找相同字符,遇到后调用删除函数。
3、删除函数也比较简单,将要删除的位置传入,将要删除后一个位置的字符复制到要删除的位置,进行覆盖。