标签:c++
题目描述反转字符串cbafed得到defabc,这就实现了整个反转。
#include <iostream>
#include <string>
using namespace std;
//将字符串s中下标from到to之间(从0开始)的字符反转
bool Reverse(char *s, int from, int to)
{
if (s==NULL||from>=to)
{
return false;
}
while(from<to)
{
int temp=s[from];
s[from++]=s[to];
s[to--]=temp;
}
return true;
}
//将字符串前面m个字符(m<字符串长度)移动到字符串尾部
bool leftRotateString(char *s, int m)
{
if (s==NULL||m<0)
{
return false;
}
int len=strlen(s);
if (m>=len)
{
return false;
}
Reverse(s,0,m-1);
Reverse(s,m,len-1);
Reverse(s,0,len-1);
return true;
}
int main()
{
char buf[30];
strcpy(buf,"abcdef");
cout<<buf<<endl;
if (leftRotateString(buf,3))
{
cout<<buf<<endl;
}
else
{
cout<<"failure!"<<endl;
}
return 0;
}标签:c++
原文地址:http://blog.csdn.net/lsh_2013/article/details/45507117