字符串循环移位

翻手法

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*reverse hand to do rotate right*/



void reversehand(char *list, int low , int high)
{	
	int tmp;
	while(low<high)
	{
		tmp = list[low];
		list[low]=list[high];
		list[high]=tmp;
		low++;
		high--;
	}
}

int main()
{
	char *list;
	int length,n;
	list = (char *)malloc(sizeof(char));
	strcpy(list,"abcdefghi");
	length = strlen(list);
	printf("how many steps you want to move to right rotate:\n");
	scanf("%d",&n);
	reversehand(list,0,length-n-1);
	reversehand(list,length-n,length-1);
	reversehand(list,0,length-1);
	printf("after right shifted %d step(s) the list is :%s\n",n,list);
	system("pause");
	return 0;
}



原创文章 5 获赞 0 访问量 2867

猜你喜欢

转载自blog.csdn.net/sy19901011/article/details/27526203