**题目:字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。
代码示例(需要改进,参考前题):**
#include<iostream>
#include<string>
using namespace std;
bool LeftCircleShift(string &str, int n)
{
if (str == ""||n<0)
return false;
int len = str.length();
if (n > len)
return false;
char *p = new char[n + 1];
for (int k = 0; k < n; k++)
{
p[k] = str[k];
}
p[n] = '\0';
int i = 0;
int j = n;
while (j < len)
{
str[i] = str[j];
i++;
j++;
}
j = 0;
while (p[j] != '\0')
{
str[i] = p[j];
i++;
j++;
}
return true;
}
int main()
{
string str = "student";
cout << "原字符串:" << str << endl;
int n = 2;
bool flag = LeftCircleShift(str, n);
cout << "旋转"<<n<<"位之后:"<<str<< endl;
}