string 字符串处理等函数操作

#include <iostream>
#include <cstring>
using namespace std;
int main(){
    string str,s,c;
    int n,a,b;
    cin>>n;
    cin>>str;
    char op;
    while(n--){

    cin>>op;
    if(op=='1'){
        cin>>s;
        str=str+s;
        cout<<str<<endl;
    }
    if(op=='2'){

       cin>>a>>b;
        c=str.substr(a,b);
       str=c;
       cout<<str<<endl;
    }
    if(op=='3'){
        cin>>a>>c;
        str.insert(a,c);
        cout<<str<<endl;
    }
    else if(op=='4'){
        cin>>c;
        if(str.find(c)<str.size()) cout<<str.find(c)<<endl;
        else cout<<"-1"<<endl;
    }
    }
   return 0;
    
}
#include<iostream>
#include<cmath>
#include<string>
#include<fstream>
using namespace std;
int n,a;
string qwq;
string c1;
string b1;
int b,c,d=-1,e;//并不全有用
int main()
{
	cin>>n;
	cin>>qwq;
	for(int i=0;i<n;i++)
	{
		cin>>a;
		if(a==1)//操作1
		{
			cin>>b1;
			qwq+=b1;
			cout<<qwq<<endl;
		}
		else if(a==2)//操作2
		{
			cin>>b>>c;
			c1=qwq.substr(b,c);
			qwq=c1;
			cout<<qwq;
			cout<<endl;
		}
		else if(a==3)//操作3
		{
			cin>>b>>b1;
			qwq.insert(b,b1);
			cout<<qwq<<endl;
		}
		else if(a==4)//操作4
		{
			cin>>b1;
			if(qwq.find(b1)<qwq.size())//找不到会返回一个诡异的数字(反正比字符串长)
			cout<<qwq.find(b1)<<endl;
			else
			cout<<-1<<endl;
		}
	}
    return 0;
}

【深基6.例6】文字处理软件 - 洛谷

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值