判断一个字符串是否为另外一个字符串旋转之后的字符串。
例如:给定s1 = AABCD和s2 = BCDAA,返回1,给定s1=abcd和s2=ACBD,返回0.
#include
#include
#include
void shiftstep(char *msg, int step)
{
assert(msg);
int len = strlen(msg);
step = step%len;
reverse(msg, msg + step - 1);
reverse(msg + step, msg + len - 1);
reverse(msg, msg + len - 1);
}
reverse(char *start, char *end)
{
assert(start);
assert(end);
while (start < end)
{
*start = *start^*end;
*end = *start^*end;
*start = *start^*end;
start++;
end--;
}
}
int find(char *str, char *substr)
{
assert(str);
assert(substr);
int len = strlen(str);
if (len == strlen(substr))
{
char *_str = (char *)malloc(2 * len + 1);
strcpy(_str, str);
strcat(_str, str);
if ((strstr(_str, substr)) != NULL)
{
return 1;
}
free(_str);
}
return 0;
}
int main()
{
char str[] = "123456";
char substr[] = "7894";
shiftstep(str, 0);
printf("%d\n", find(str, substr));
return 0;
}