蓝桥杯 字符串编辑 字符串操作

问题描述
  从键盘输入一个字符串(长度<=40个字符),并以字符 ’.’ 结束。编辑功能有:
  1 D:删除一个字符,命令的方式为: D a 其中a为被删除的字符,例如:D s 表示删除字符 ’s’ ,若字符串中有多个 ‘s’,则删除第一次出现的。
  2 I:插入一个字符,命令的格式为:I a1 a2 其中a1表示插入到指定字符前面,a2表示将要插入的字符。例如:I s d 表示在指定字符 ’s’ 的前面插入字符 ‘d’ ,若原串中有多个 ‘s’ ,则插入在最后一个字符的前面。
  3 R:替换一个字符,命令格式为:R a1 a2 其中a1为被替换的字符,a2为替换的字符,若在原串中有多个a1则应全部替换。
  在编辑过程中,若出现被改的字符不存在时,则给出提示信息。
输入格式
  输入共两行,第一行为原串(以’.’结束),第二行为命令(输入方式参见“问题描述” 。
输出格式
  输出共一行,为修改后的字符串或输出指定字符不存在的提示信息。
样例输入
This is a book.
D s
样例输出
Thi is a book.
输入输出样例解释
  命令为删去s,第一个在字符中出现的s在This中,即得到结果。
题意好像有问题,"在编辑过程中,若出现被改的字符不存在时,则给出提示信息"好像没用,假定所有被改的字符都存在。
基础的字符串操作,一步一步模拟着来。
被char读入换行之类的其他字符坑过有阴影,我习惯用string读入。
参考自https://blog.csdn.net/vivenn/article/details/79615847
 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