研0 冲刺算法竞赛 day11 P5734 【深基6.例6】文字处理软件

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.

  1. substr 生成子串,输入位置和长度

  2. insert 在字符串中插入字符串

  3. find 查找字符串中某个字符串的位置并返回它的位置

  • 15
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值