当前位置:网站首页>Determine whether a string is rotated from another string

Determine whether a string is rotated from another string

2022-06-13 08:10:00 Stupid little bird learning code

// Method 1 
int is_left_move(char* s1,char* s2)
{
	int len = strlen(s1);
	int i = 0;
	for(i=0;i<len;i++)
	{
		left_move(s1,1);// There are several reasons why the following rotation is not possible   It changes after the rotation 
		                // The next rotation is not the result of the original string rotation  
		int ret = strcmp(s1,s2);// Same back 0  Different back 1
		if(ret == 0)
		{
			return 1;
		}
		// You can't just else  After all the judgments have been made  
	}
	return 0;
 } 
 

// Method 2 
//abcdefabcdef  All the inversion results in a subset of this string  
int is_left_move_2(char* str1,char* str2)
{
	int len1 = strlen(str1); 
	int len2 = strlen(str2);
	if(len1 != len2)
	{
		return 0;
	}
	//1. stay str1 Append a... To the string str1 character string 
	//strcat   This library function   You can't add to yourself   Will be able to \0 Get rid of  
	//strncat 
	strncat(str1,str1,len1);
	//2. Judge str2 Whether the string pointed to is str Substring of the string pointed to 
	//strstr- Find Zichuan 
	char* ret = strstr(str1,str2);// stay str1 In looking for str2  Return null pointer not found  
	if(ret == NULL)
	{
		return 0;
	} 
	else
	{
		return 1;
	}
	return 0; 
}


// The main function 
int main()
{
	char arr1[] = "abcdef";//char arr1 = "abcdef"  This will cause an error   The reason is that by this definition  
	char arr2[] = "cdefa";// Is a defined constant string   No modification allowed   So it is still defined as an array  
	//int ret = is_left_move(arr1,arr2);
	int ret = is_left_move_2(arr1,arr2); 
	if(ret == 1)
	{
		printf("YES\n");
	}
	if(ret == 0)
	{
		printf("NO\n");
	}
	return 0; 
 } 

原网站

版权声明
本文为[Stupid little bird learning code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130806409172.html