String left rotation & Determine whether a string is the string after another string rotation

1. String reverse order function

char* rot(char* const arr, int k)
{
    
    
	assert(arr);
	//1、找到末尾字符串 并存起来
	int sz = strlen(arr) - 1;
	int tmp = arr[sz];
	//2、把字符串后移(从末尾到头覆盖)
	for (sz; sz > 0; sz--)
	{
    
    
		arr[sz] = arr[sz - 1];
	}
	//3、把末尾字符放到开头
	arr[0] = tmp;
	//4、调整k的值
	k--;
	//5、判读 递归/结束
	if (k == 0)
	{
    
    
		return arr;
	}
	if (k > 0)
	{
    
    
		rot(arr, k);
	}
}

char* rot(char* const arr, int k);

Function:
Pass parameter one (address arr of the first character of the string) and parameter two (integer number k), left-rotate the k characters in the string arr , and return the address of the first character of the string after left-rotation .

Parameter 1:
Type: char *
Param content: the array name of the string array to be left-rotated , ( the address of the first character ).

Parameter 2:
Type: int
Parameter content: the number of characters to be left-handed in the string .

Return type:
type: char
return content: the address of the first character after left-handed string .

2. Determine whether a string is a string function after another string is rotated

#include<stdio.h>
#include<assert.h>
#include<string.h>
char* rot(char* const arr, int k)
{
    
    
	assert(arr);
	//1、找到末尾字符串 并存起来
	int sz = strlen(arr) - 1;
	int tmp = arr[sz];
	//2、把字符串后移(从末尾到头覆盖)
	for (sz; sz > 0; sz--)
	{
    
    
		arr[sz] = arr[sz - 1];
	}
	//3、把末尾字符放到开头
	arr[0] = tmp;
	//4、调整k的值
	k--;
	//5、判读 递归/结束
	if (k == 0)
	{
    
    
		return arr;
	}
	if (k > 0)
	{
    
    
		rot(arr, k);
	}
}

int equal(char* arr1, char* arr2)
{
    
    
	if (strlen(arr1) == strlen(arr2))//如果长度相等,进入判断
	{
    
    
		int k = 0;
		for (k = 0; k < strlen(arr1); k++)//循环,使出现arr1左旋的所有情况
		{
    
    
			rot(arr2, 1);
			int ret = strcmp(arr1, arr2);//对arr1所有左旋情况进行判断,看是否与arr2相等
			//int x = ret;
			if (ret == 0)//相等返回1
			{
    
    
				return 1;
			}
		}
		return 0;//若所有情况都不相等,返回0
	}
	else//如果长度不相等,直接返回0
	{
    
    
		return 0;
	}
}

int main()
{
    
    
	/*char arr[] = "ABCDEF";
	int k = 0;
	scanf_s("%d", &k);
	printf("左旋前:%s", arr);
	char* ret = rot(arr, k);
	printf("左旋后:%s", arr);
	return 0;*/
	char arr1[] = "ABCD";
	char arr2[] = "CDAB";
	int n = equal(arr1, arr2);
	if (n == 1)
	{
    
    
		printf("左旋可得到");
	}
	else
	{
    
    
		printf("左旋得不到");
	}
}

int equal(char* arr1, char* arr2);

Function:
Pass in parameter 1 (address arr1 of the first character of the original string) and parameter 2 (address arr2 of the first character of the string to be compared), and
determine whether the string (arr2) is the character after the left rotation of the original string (arr1) String, returns 1 if yes; returns 0 if not.

Parameter 1:
Type: char *
Param content: the array name of the original string array , ( the address of the first character ).

Parameter 2:
Type: char * Pass parameter content: the array name
of the string array to be compared , ( the address of the first character ).

Return type:
Type: int
Return content: Return 1 if yes ; return 0 if not .

Guess you like

Origin blog.csdn.net/qq_56847032/article/details/117391831