方法一.直接写出代码旋转逻辑
方法二:利用划分法将字符串划分成三分,[0,num-1],[num,len-1],[0,len-1];分别将其三个划分逆置,便会得到想要旋转的字符串
方法三:利用构建双倍串的方法,再利用系统函数来判断双倍串中是否有第二个字符串(strcpy,strstr,strcat)
详细逻辑请看代码哦,亲!
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
#pragma warning(disable:4996)
//void Reverse(char *str, int start, int end)
//{
// while (start<end)
// {
// str[start] ^= str[end];
// str[end] ^= str[start];
// str[start] ^= str[end];
// start++; end--;
// }
//}
/oid LeftRoate(char *str, int len, int num)
//{
// assert(str);//合理性校验
// assert(len > 0);
// assert(num > 0);
// num %= len;//合法去重
// //方法一
// while (num)
// {
// char temp = str[0];
// //i最大为len-1
// int i = 0;
// for (; i < len - 1; i++)
// {
// str[i] = str[i + 1];
// }
// str[i] = temp;
// num--;
// }
旋转字符串方法二
划分字串
[0,num-1],
//Reverse(str, 0, num - 1);
[num,len-1],
//Reverse(str, num, len-1);
// //[0,num]
//Reverse(str, 0, len - 1);
//方法三双倍串(牺牲空间换取时间)直接在main函数中利用系统库函数实现
//}
int main()
{
//双倍串利用系统库函数实现
char str1[] = "1234abcd"; //d1234abc, d1234abcd
char str2[] = "abcd1234";
int len = strlen(str1);
char *double_string = (char*)malloc(2 * len + 1);
if (double_string == NULL){
return -1;
}
strcpy(double_string, str1);
strcat(double_string, str1);
printf("double string : %s\n", double_string);
if (strstr(double_string, str2) != NULL){
printf("yes\n");
}
else{
printf("no!\n");
}
free(double_string);
//前两种方法都有可以用下列代码
//int len = strlen(str1);
//int i = 0;
//for (; i < len; i++)
//{
// if (strcmp(str1, str2) == 0)
// {
// printf("yes!\n");
// break;
// }
// LeftRoate(str1, len, 1);
//}
//if (i == len)
//{
// printf("no!\n");
//}
system("pause");
return 0;
}