C++删除字符串中的子串erase以及substr

题目:

string str = "62354iijnjnij26758667ijn615468565ij"

将子字符串"ijn"全部删除
方法1:
该方法有个缺陷,它会将字符串中只要是ijn顺序的字符都会删除

	int pos = 0;
	while (string::npos != (pos = str.find("ijn")) )
	{
		str.erase(pos, 3);
	}
	cout << str << endl;

删除字符串中的子串
输入2个字符串S1和S2,要求删除字符串S1中出现的所有子串S2,即结果字符串中不能包含S2。
输入格式:
输入在2行中分别给出不超过80个字符长度的、以回车结束的2个非空字符串,对应S1和S2。
输出格式:
在一行中输出删除字符串S1中出现的所有子串S2后的结果字符串。
输入样例:

Tomcat is a male ccatat
cat

输出样例:

Tom is a male
#include<iostream>
using namespace std;
int main()
{
	string s1,s2;
	getline(cin,s1);
	getline(cin,s2);
	while(s1.find(s2)!=string::npos)
	{
		s1.erase(s1.find(s2),s2.length());
	}
	cout<<s1<<endl;
}

1、string.erase(pos,n) //删除从pos开始的n个字符 string.erase(0,1); 删除第一个字符
2、string.erase(pos) //删除pos处的一个字符(pos是string类型的迭代器)
3、string.erase(first,last) //删除从first到last中间的字符(first和last都是string类型的迭代器)

   s.erase(s.begin()+1,s.end()-1);
下面给你一个例子:
// string::erase
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str ("This is an example phrase.");
  string::iterator it;

  // 第(1)种用法
  str.erase (10,8);
  cout << str << endl;        // "This is an phrase."

  // 第(2)种用法
  it=str.begin()+9;
  str.erase (it);
  cout << str << endl;        // "This is a phrase."

  // 第(3)种用法
  str.erase (str.begin()+5, str.end()-7);
  cout << str << endl;        // "This phrase."
  return 0;
}
```c
转载**https://www.cnblogs.com/liyazhou/archive/2010/02/07/1665421.html**
**方法2**

	int i = 0;
	while (i < str.size()-2)
	{
		if ('i' == str[i])
		{
			if ("ijn" == str.substr(i,3))
			{
				str.erase(i,3);
				continue;
			}
			//if ('j' == str[i + 1] && 'n' == str[i + 2] )
			//{
			//	str.erase(i,3);
			//	continue;
			//}
		}
		++i;
	}
	cout << str << endl;
**方法3**

	int pos = 0;
	while (pos < str.size() - 2) {
		if ("ijn" == str.substr(pos, 3)) {
			str.erase(pos, 3);
		}
		else {
			++pos;
		}
	}
	cout << str << endl;
**方法4**
```c
	string::iterator it = str.begin();
	while (it != str.end() - 2)
	{
		if ('i' == *it)
		{
			if ('j'  == *(it + 1) && 'n' == *(it + 2))
			{
				str.erase(it - str.begin(),3);
				continue;
			}
			//if ("ijn" == str.substr(it - str.begin(), 3))
			//{
			//	str.erase(it - str.begin(), 3);
			//	continue;
			//}
		}
		++it;
	}
	cout << str << endl;

转载https://blog.csdn.net/yishizuofei/article/details/79059804
补充:gets()函数来输入字符串,用fget()函数来输入字符串。gets()函数在换行时结束输入,不会把’\n’纳入字符串内容,fget()函数也是在换行时结束输入,但是会把’\n’作为字符串内容的一部分。
substr

string s="0123456789";
string str1=s.substr(5);只有一个数字5表示从下标为5开始一直到结尾
str1="56789"
string str2=s.substr(5,3);从下标为5开始截取长度为3位
str2="567"

题目
字符串循环左移(20 分)
输入一个字符串和一个非负整数N,要求将字符串循环左移N次。
输入格式:
输入在第1行中给出一个不超过100个字符长度的、以回车结束的非空字符串;第2行给出非负整数N。
输出格式:
在一行中输出循环左移N次后的字符串。
输入样例:

Hello World!
2

输出样例:

llo World!He

C++

#include<iostream>
using namespace std;
int main()
{
	string s;
	getline(cin,s);
	int a;
	cin>>a;
	a=a%s.length();
	string s1=s.substr(0,a);
	string s2=s.substr(a);
	cout<<s2<<s1;
}

C语言
转载于https://blog.csdn.net/mikucsdn/article/details/80606282

#include<stdio.h>
#include<string.h>
int main(){
	char c[120];
	int n,len;
	gets(c);
	scanf("%d",&n);
	len=strlen(c);
	for(int i=0;i<len;i++){
		printf("%c",c[(n+i)%len]);
	}
}

不用改变原字符数组,直接输出即可。向右移动改为输出c[(len-n+i)%len]即可。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值