7-48 use of "append()erase() to move the string left circularly

Enter a string and a non negative integer N, which requires the string to be rotated left N times.

Input format:
Enter a non empty string ending with carriage return with a length of no more than 100 characters in line 1, and a non negative integer N in line 2.

Output format:
Output the string after N left shifts of the loop in one line.

Input example:

Hello World!
2

Output example:

llo World!He

Knowledge points

Analysis of from

  1. Non negative integer N, indicating the case of N=0. If N=0, output the original string
  2. Cycle left shift: remove N characters from the first character. Cycle to determine whether N is greater than the string length. If N is less than the string length, move N characters to the left. If N is greater than the string length, it is easy to think of taking R=N% of the string length and moving R characters to the left. If N is equal to the string length, output the original string
  3. This is a detail that most people tend to overlook at the beginning: input examples include spaces! So C + + can use getline to accept spaces.
  4. append(): the append function appends a character or string to the end of a string.
    erase(): delete

1) . add C-string after string

string s ="hello "; 
const char *c = "out here ";
s.append(c); // Connect the c type string s to the end of the current string
s = "hello out here";

2) . add a part of C-string after the string

string s="hello ";
const char *c = "out here ";
s.append(c,3); // Connect the first n characters of string s of type c to the end of the current string
s = "hello out";

3) . add string after string

string s1 = "hello "; 
string s2 = "wide "; 
string s3 = "world ";
s1.append(s2); 
s1 += s3; //Connect string s to the end of the current string
s1 = "hello wide "; 
s1 = "hello wide world ";

4) . add a part of string to the end of string

string s1 = "hello ",s2 = "wide world ";
s1.append(s2, 5, 5); ////Connect 5 characters from 5 in string s2 to the end of the current string
s1 = "hello world";
string str1 = "hello ", str2 = "wide world ";
str1.append(str2.begin()+5, str2.end()); //Connect the part between s2's iterator begin()+5 and end() to the end of the current string
str1 = "hello world";

5) . add more than one character after string

string s1 = "hello ";
s1.append(4,'!'); //Add 4 characters at the end of the current string!
s1 = "hello !!!!";

Original link

Code 1

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s,t;
	int N,R;
	getline(cin,s);
	cin>>N;
	if(N>0&&N<s.length()) //0--2 t=He ; s=llo World! ;  s=llo World!He
	{                     //Cut storage; delete; splice 
		t.append(s,0,N);/*Append N characters from subscript 0 in s to temporary string t*/
		s.erase(0,N);/*Delete N characters from subscript 0 in s*/
		s.append(t);/*Append temporary string t to s*/
	}
	else if(N>s.length())
	{
		R=N%s.length();/*Remainder*/
		t.append(s,0,R);
		s.erase(0,R);
		s.append(t);
	}
	cout<<s;
	return 0;
}

AC code

But I don't know what it means yet
Source code

#include <iostream>
#include <string>
using namespace std;
int main() {
	string Ori;
	getline(cin, Ori);	//Get the original string
	int N;
	cin >> N;	//Get left shift digit
	N = N % Ori.size();		//When N exceeds the length of the original string
	string Tmp1(Ori, N, Ori.size());	//Cut the second half to get the first string
	string Tmp2(Ori, 0, N);		//Cut the first half to get the second string
	Ori = Tmp1 + Tmp2;	//Concatenate the result string
	cout << Ori;
	return 0;
}

Own code

#include<iostream>
#include<cstring>
using namespace std;

int main(){
	string str;
	getline(cin,str);
	int n;cin>>n;
	int len=str.size();
	n=n%len;
	for(int i=0;i<len;i++){
		cout<<str[(i+n)%len];
	}
}
Published 62 original articles, won praise 2, visited 485
Private letter follow

Tags: less

Posted on Wed, 05 Feb 2020 02:54:31 -0800 by techevan