题目:
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如输入字符串“abcdefg”和数字2,该函数将返回左旋转2位得到的结果“cdefgab”。
方法一
分析
利用string字符串的substr函数截取子串,再进行黏贴。
源码
#include<iostream>
#include<string>
using namespace std;
void Function(string &str, unsigned int n)
{
if (str.empty() || str.length() <= n || n <= 0)
{
cout << "不满足旋转条件" << endl;
return;
}
int len = str.length();
string bStr = str.substr(0, n);
string aStr = str.substr(2);
str = aStr + bStr;
}
void test1()
{
cout << "=============test1:abcdefg, 2=================" << endl;
string str = "abcdefg";
Function(str, 2);
cout << str << endl;
}
void test2()
{
cout << "=============test1:abcdefg, 0=================" << endl;
string str = "abcdefg";
Function(str, 0);
cout << str << endl;
}
void test3()
{
cout << "=============test1:abcdefg, 8=================" << endl;
string str = "abcdefg";
Function(str, 8);
cout << str << endl;
}
void test4()
{
cout << "=============test1:空, 2=================" << endl;
string str;
Function(str, 8);
cout << str << endl;
}
int main()
{
test1();
test2();
test3();
test4();
system("pause");
return 0;
}
=============test1:abcdefg, 2=================
cdefgab
=============test1:abcdefg, 0=================
不满足旋转条件
abcdefg
=============test1:abcdefg, 8=================
不满足旋转条件
abcdefg
=============test1:空, 2=================
不满足旋转条件
请按任意键继续. . .
方法二
分析
以“abcdefg”为例,我们可以把它分为两部分。由于想把它的前两个字符移到后边,我们可以把前两个字符分到第一部分,把后边的字符分到第二部分。分别翻转这两部分,也是得到“bagfedc”。接下来我们再翻转整个字符串,得到的“cdefgab”刚好就是把原始字符串左旋转2位的结果。
源码
#include<iostream>
using namespace std;
void Reverse(char *arr, int length)
{
if (arr == nullptr || length <= 1)
return;
char *pBegin = arr;
char *pEnd = arr + length - 1;
while (pBegin < pEnd)
{
char temp = *pBegin;
*pBegin = *pEnd;
*pEnd = temp;
++pBegin;
--pEnd;
}
}
void LeftRotateString(char *arr, int n)
{
if (arr == nullptr || n <= 0)
{
cout << "字符串不满足翻转条件" << endl;
return;
}
int len = static_cast<int>(strlen(arr));
if ( n >= len)
{
cout << "字符串不满足翻转条件" << endl;
return;
}
Reverse(arr, n);
Reverse(arr + n, len - n);
Reverse(arr, len);
}
void test11()
{
cout << "=============test1:abcdefg, 2=================" << endl;
char str[] = "abcdefg";
LeftRotateString(str, 2);
if (str == nullptr)
cout << "字符串为空" << endl;
else
cout << str << endl;
}
void test12()
{
cout << "=============test2:abcdefg, 0=================" << endl;
char str[] = "abcdefg";
LeftRotateString(str, 0);
if (str == nullptr)
cout << "字符串为空" << endl;
else
cout << str << endl;
}
void test13()
{
cout << "=============test3:abcdefg, 8=================" << endl;
char str[] = "abcdefg";
LeftRotateString(str, 8);
if (str == nullptr)
cout << "字符串为空" << endl;
else
cout << str << endl;
}
void test14()
{
cout << "=============test4:空, 2=================" << endl;
char *str = nullptr;
LeftRotateString(str, 2);
if (str == nullptr)
cout << "字符串为空" << endl;
else
cout << str << endl;
}
void test15()
{
cout << "=============test5:abcdefg, 1=================" << endl;
char str[] = "abcdefg";
LeftRotateString(str, 1);
if (str == nullptr)
cout << "字符串为空" << endl;
else
cout << str << endl;
}
int main()
{
test11();
test12();
test13();
test14();
test15();
cout << endl;
system("pause");
return 0;
}
运行结果:
=============test1:abcdefg, 2=================
cdefgab
=============test2:abcdefg, 0=================
字符串不满足翻转条件
abcdefg
=============test3:abcdefg, 8=================
字符串不满足翻转条件
abcdefg
=============test4:空, 2=================
字符串不满足翻转条件
字符串为空
=============test5:abcdefg, 1=================
bcdefga
请按任意键继续. . .