北邮oj140字符串操作

在这里插入图片描述
在这里插入图片描述
本题本想使用字符数组实现,但是关于替代的情况,字符数组的长度变化,和元素移动没写出方法。遂专用STL,只是注意reverse(),string.replace()的使用方法

#include<bits/stdc++.h>
using namespace std;
string s;
int main(){
	while(cin>>s){
		int t;
		scanf("%d",&t);
		while(t--){
			int c;//判断
			int i,len;
			scanf("%d",&c); 
			if(c==0){
				scanf("%d %d",&i,&len);
				reverse(s.begin()+i,s.begin()+i+len);//c++的reverse()函数,可以反转字符串和字符数组 
		}
			else if(c==1){
				string str;
				scanf("%d %d",&i,&len);
				cin>>str;
				s.replace(i,len,str);	//string的replace()一步解决			
			}
			for(int i=0;i<s.length();i++){
					printf("%c",s[i]);
				}
				printf("\n");
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_37762592/article/details/88594130
今日推荐