P5734 【深基6.例6】文字处理软件 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
思路:根据1,2,3,4分别处理,使用string的函数进行处理
代码:
#include<iostream>
#include<string>
using namespace std;
//void handle(int flag,string str,string steps)
//{
// //string str1;
// //str1.resize(150);
// int len = steps.size(), j=0;
// //cout << len;
// string temp;
//
//}
int main()
{
int N,flag;
cin >> N;
string str,steps,tep;
//str.resize(150);
//steps.resize(150);
cin >> str;//处理str对象,ILOVE
while (N--)
{
cin >> flag;
//cout << steps.size();
if (flag == 1)//后接插入,在文档后面插入字符串并输出
{
cin >> steps;
str += steps;
cout << str << endl;
}
if (flag == 2)//截取文档部分,只保留文档中从第a到a+b
{
int a, b;
cin >> a >> b;
tep = str.substr(a, b);//生成子串,输入位置和长度
str = tep;
cout << str << endl;
}
if (flag == 3)//第a个字符前插入字符串str
{
int a;
cin >> a;
cin >> tep;
str.insert(a, tep);
cout << str << endl;
}
if (flag == 4)//查找
{
cin >> tep;
int len = str.size();
if (str.find(tep) < len)
cout << str.find(tep) << endl;
else
cout << -1 << endl;
}
}
return 0;
}
总结:
1.注意输入可以分别获取,不用在字符串整体处理
2.