码迷,mamicode.com
首页 > 其他好文 > 详细

字符串移位

时间:2014-09-28 12:28:20      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   os   ar   for   sp   2014   art   

字符串循环移位:假设有一串字符串a,b,c,d,e,f,g,h,向左循环移位2为,得c,d,e,f,g,h,a,b。

#include<iostream>
using namespace std;

void reverse(char* a, int start, int len){
	int count = 0;

	for(int i = start, j = start + len -1; ; ++i, --j){
		if((++count) <= len/2){
			a[i] = a[i] + a[j];
			a[j] = a[i] - a[j];
			a[i] = a[i] - a[j];
		}else{
			break;
		}
	}
}

void transposition(char* a, int n, int len){
	reverse(a,0,len);
	reverse(a,0,len-n);
	reverse(a,len-n,n);
}



void main(){
	char a[] = {'a','b','c','d','e','f','g','h'};
	int len = sizeof(a);
	transposition(a,9%len,len);
	for(int i = 0; i < len; ++i){
		cout<<a[i]<<" ";
	}
	cout<<endl;
}
结果:

bubuko.com,布布扣


字符串移位

标签:blog   http   io   os   ar   for   sp   2014   art   

原文地址:http://blog.csdn.net/huojushou1128/article/details/39637461

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!