Implement a function, L character string k

Implement a function to be left in the k character strings.
ABCD left a character get BCDA
ABCD left two characters get CDAB

left_move void (STR char *, int K)
{}
[] solving ideas
passed desired number of parameters L, L each variable is defined tmp = str [0], the use of a loop array for the character, Fu to the former. Finally, the last assignment to an array tmp. Complete a left-handed

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void left_move(char *str, int len, int num)
{
	int i = 0;
	for (i = 0; i < num; i++)
	{
		int j = 0;
		char tmp = str[0];//每次旋转都要定义tmp
		for (j = 0; j < len; j++)
		{
			str[j] = str[j + 1];
		}
		str[len - 1] = tmp;
	}
}
int main()
{
	char arr[] = "ABCD";
	int len = strlen(arr);
	int num = 0;
	printf("请输入你要旋转几位\n");
	scanf("%d", &num);
	while (num<1 || num>len - 1)//输入错误,重新输入
	{
		printf("输入错误,请重新输入!\n");
		scanf("%d", &num);
	}
	left_move(arr, len,num);
	printf("旋转后:%s\n", arr);
	system("pause");
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44936160/article/details/91634209