Growth programmer trip --C language character rotation function to achieve

Growth programmer trip --C language character rotation function to achieve

Topic Introduction

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

method one

Cyclic shift of k characters that require rotation in order to rotate, to save a first rotating character up to the back of a moving forward, and then saved in the last character is assigned, so that continuous k cycle times.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void left_remove(char* p, int n, int len)
{
 	int i = 0;
 	for (i = 0; i < n; i++)
 	{
  		int j = 0;
  		int tmp = p[0];
  	for (j = 0; j < len - 1; j++)
  	{
   		p[j] = p[j + 1];
  	}
  		p[len - 1] = tmp;
 	}
}
int main()
{
 	char p[] = "ABCD";
 	int n = 0;
 	int len = strlen(p);
 	printf("左旋前字符串为:>");
 	printf("%s\n", p);
 	printf("左旋几位:>");
 	scanf_s("%d", &n);
 	while (n<1 || n>len - 1)
 	{
  	if (n == len || n == 0)
  	{
   		printf("左旋后字符串为:>");
   		printf("%s", p);
   		return 0;
  	}
  	else
  	{
   		printf("输入有误!\n");
   		scanf_s("%d", &n);
  	}
 }
 	left_remove(p, n, len);
 	printf("旋转后:%s\n", p);
 	system("pause");
 	return 0;
}

Here Insert Picture Description

Method Two

Commonly known as the three-step rotation method:
first character to be left-handed rotation, then the character is not left-handed rotation, and finally all the characters to rotate, to obtain the desired character. AB characters such as ABCD To get the rotation should be: CDAB in accordance with the above mentioned is the BACD-> BADC-> CDAB not hard to see income is the same.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void reserve(char* left,char* right)
{
 	while (left < right)
 	{
  		char tmp = *left;
  		*left = *right;
  		*right = tmp;
  		left++;
  		right--;
 	}
}
void left_remove(char* p, int n, int len)
{
 	reserve(p, p + n - 1);
 	reserve(p + n, p + len - 1);
 	reserve(p, p + len - 1);
}
int main()
{
 	char p[] = "ABCD";
 	int n = 0;
 	int len = strlen(p);
 	printf("左旋前字符串为:>");
 	printf("%s\n", p);
 	printf("左旋几位:>");
 	scanf_s("%d", &n);
 	while (n<1 || n>len - 1)
 	{
  	if (n == len || n == 0)
 	{
   		printf("左旋后字符串为:>");
   		printf("%s", p);
   		return 0;
  	}
 	else
  	{
   		printf("输入有误!\n");
   		scanf_s("%d", &n);
  	}
 }
 	left_remove(p, n, len);
 	printf("旋转后:%s\n", p);
 	system("pause");
 	return 0;
}

Here Insert Picture Description

Method Three

Pointer realization

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void left_remove(char arr[], int n)     
{
 	char arr1[5] = { 0 };
 	char* mov1 = arr;
 	char* mov2 = arr;
 	char* res1 = arr1;
 	char* res2 = arr1;
 	while (n--)
 	{
  		*res1++ = *mov2++;
 	}
 	while (*mov2)
 	{
  		*mov1++ = *mov2++;
 	}
 	while (*mov1)
 	{
  		*mov1++ = *res2++;
 	}
}
int main()
{
 	char p[] = "ABCD";
 	int n = 0;
 	int len = strlen(p);
 	printf("左旋前字符串为:>");
 	printf("%s\n", p);
 	printf("左旋几位:>");
 	scanf_s("%d", &n);
 	while (n<1 || n>len - 1)
 {
  	if (n == len || n == 0)
  	{
   		printf("左旋后字符串为:>");
   		printf("%s", p);
   		return 0;
  	}
  	else
  	{
   		printf("输入有误!\n");
   		scanf_s("%d", &n);
  	}
 }
 	left_remove(p, n);
 	printf("旋转后:%s\n", p);
 	system("pause");
 	return 0;
}

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/wuweiwuju___/article/details/93842138