您的位置:首页 > 其它

字符串循环左移

2016-05-01 14:33 357 查看
字符串循环左移:

给定一个字符串,要求把S的前k个字符移动到S的尾部,如把字符串"abcdef"前面的2个字符'a','b'移动到字符串的尾部,得到新字符"cdefab",即字符串循环左移k位。

程序实现:

/***************************************
FileName ReverseString.cpp
Author : godfrey
CreatedTime : 2016/5/1
****************************************/
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

void ReverseString(char* str,int from,int to){
char t;
while(from < to){
t = str[from];
str[from++] = str[to];
str[to--] = t;
}
}

void LeftRotateString(char* str,int n,int m){
m %= n;
ReverseString(str,0,m-1);
ReverseString(str,m,n-1);
ReverseString(str,0,n-1);
}
int main()
{
char str[] = "abcdef";
cout<<str<<endl;
LeftRotateString(str,strlen(str),2);
cout<<"-------------LeftRotate 2----------"<<endl;
cout<<str<<endl;
return 0;
}


运行结果:



转载请注明出处:

C++博客园:godfrey_88

http://www.cnblogs.com/gaobaoru-articles/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: