1 #include <bits/stdc++.h> 2 using namespace std; 3 int main() { 4 string s; 5 getline(cin, s); 6 string a, b, c; 7 cin >> a; 8 int len = s.length(); 9 int t; 10 if (a[0] == ‘D‘) { 11 cin >> b; 12 t = 0; 13 for (int i = 0; i < len; i++) { 14 if (s[i] == b[0]) { 15 break; 16 } 17 t++; 18 } 19 for (int i = t + 1; i < len; i++) { 20 s[i - 1] = s[i]; 21 } 22 len--; 23 for (int i = 0; i < len; i++) { 24 cout << s[i]; 25 } 26 cout << endl; 27 } else if (a[0] == ‘I‘) { 28 cin >> b >> c; 29 t=0; 30 for (int i = 0; i < len; i++) { 31 if (s[i] == b[0]) { 32 t = i; 33 } 34 } 35 for (int i = len - 1; i >= t; i--) { 36 s[i + 1] = s[i]; 37 } 38 len++; 39 s[t] = c[0]; 40 for (int i = 0; i <= len ; i++) { 41 cout << s[i]; 42 } 43 cout << endl; 44 } else if (a[0] == ‘R‘) { 45 cin >> b >> c; 46 t = 0; 47 for (int i = 0; i < len; i++) { 48 if (s[i] == b[0]) { 49 s[i] = c[0]; 50 } 51 } 52 for (int i = 0; i < len; i++) { 53 cout << s[i]; 54 } 55 cout << endl; 56 } 57 return 0; 58 } 59
也可以用c++库函数做,不过这些函数参数好多,容易混淆,我还是习惯手动模拟。
参考自https://blog.csdn.net/Z_122113/article/details/104353544
1 #include <bits/stdc++.h> 2 using namespace std; 3 string str; 4 char edit; 5 char a1, a2; 6 int main() { 7 getline(cin, str); 8 cin >> edit; 9 str = str.substr(0, str.find_first_of(".") + 1); 10 string tempstr = str; 11 switch (edit) 12 { 13 case ‘D‘:cin >> a1; str = str.erase(str.find_first_of(a1), 1); break; 14 case ‘I‘:cin >> a1 >> a2; str = str.insert(str.find_last_of(a1), 1, a2); break; 15 case ‘R‘:cin >> a1 >> a2; replace(str.begin(), str.end(), a1, a2); break; 16 default: 17 break; 18 } 19 if (str == tempstr) 20 cout << "no exist"; 21 else cout << str; 22 return 0; 23 }
原文:https://www.cnblogs.com/fx1998/p/12684688.html