代码:
#include <iostream>
using namespace std;
//翻转字符数组
void Reverse(char* pBegin, char* pEnd)
{
if (pBegin == nullptr || pEnd == nullptr)
return;
while (pBegin < pEnd)
{
char temp = *pBegin;
*pBegin = *pEnd;
*pEnd = temp;
--pEnd;
++pBegin;
}
return;
}
//
char* ReveseOrder(char* pString, int index)
{
if (pString == nullptr)
return nullptr;
char* pIndex = pString;
while (--index)
{
pIndex++;
}
//翻转左边
Reverse(pString, pIndex);
char* pEnd = ++pIndex;
while (*pEnd != '\0')
++pEnd;
//翻转右边
Reverse(++pIndex, --pEnd);
//翻转整个字符串
Reverse(pString, pEnd);
return pString;
}
int main()
{
char String[] = "abcdefg";
int index = 1;
cout << "原字符串: " << String<<endl;
ReveseOrder(String,index);
int nlength = sizeof(String) / sizeof(char);
cout <<"左旋后: "<< String;
cout << endl;
system("pause");
return 0;
}
测试: