P5734 【深基6.例6】文字处理软件

#include <bits/stdc++.h>
#include<string.h>
using namespace std;
int main(){
    //1.输入一个整数和最开始的字符串
    int q;string str;
    cin>>q;
    cin>>str;//输入字符串

    int a;string temp;//临时字符串
    for(int i=0;i<q;i++){
        //判断a是否为哪一个选项
        cin>>a;
        if(a==1){
            cin>>temp;
            str=str+temp;
            cout<<str<<endl;
        }
        if(a==2){
            int number1,number2;
            cin>>number1>>number2;
            str=str.substr(number1,number2);
            cout<<str<<endl;
        }
        if(a==3){
            int number3;
            cin>>number3;
            string d;
            cin>>d;
            str=str.insert(number3,d);//在字符串的确认位置下插入新的字符串
            cout<<str<<endl;
        }
        if(a==4){
            string e;
            cin>>e;
            int x=str.find(e);//查找字符串a的位置
            if(x==string::npos){
                cout<<-1<<endl;
            }
            else
            cout<<x<<endl;
        }
    }
    return 0;

}

知识点:1.字符串拼接可以直接进行加法运算

2.substr(pos1,pos2)可以截取从位置1到位置2的字符串

3.insert(pos,str)可以从指定位置插入新的字符串

4.find(s)可以在源字符串中找出是否包含s字符串中的首次出现的位置,并进行返回,如果不存在,即返回string::npos

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值