其他分享
首页 > 其他分享> > C语言实现对字符串的左旋和右旋

C语言实现对字符串的左旋和右旋

作者:互联网

1:对字符串的左旋num个字符:

void Reverse(char *left, char *right){
	assert(left != NULL&&right != NULL);
	while (left < right){
		char tmp = *left;
		*left = *right;
		*right = tmp;
		left++;
		right--;
	}
}
void  left_remove(char *string,unsigned int num){
	while (num>strlen(string)){
		return;
	}
	Reverse(string, string + (num - 1));
	Reverse(string + num, string + (strlen(string) - 1));
	Reverse(string, string + (strlen(string) - 1));
}

2:对字符串的右旋num个字符:

void right_remove(char *string, unsigned int num){
	while (num>strlen(string)){
		return;
	}
	Reverse(string + (strlen(string) -num), string + (strlen(string) - 1));
	Reverse(string, string + (strlen(string) - num-1));
	Reverse(string, string + (strlen(string) - 1));

}

3:判断在目标字符串中是否出现源字符串(可以是经过左旋后的字符串)

int  judge1(char *string1, char *string2){
	if (strlen(string1) != strlen(string2)){
		return 0;
	}
	strncat(string1, string1, strlen(string1));
	if (strstr(string1, string2)!=NULL){
		return 1;
	}
	return 0;
}

strncat函数是将string1字符串的strlen(string1)个大小的字符拼接到string1字符串后。

标签:char,right,string,C语言,右旋,num,左旋,strlen,string1
来源: https://blog.csdn.net/qq_44370562/article/details/90203642