判断一个字符串是否为另外一个字符串旋转之后的字符串。
例如:
给定s1 =AABCD和s2 = BCDAA,返回1。
给定s1=abcd和s2=ACBD,返回0。
AABCD左旋一个字符得到ABCDA
AABCD左旋两个字符得到BCDAA
AABCD右旋一个字符得到DAABC
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int comper(char* a, char* b, int n)
{
char t[100] = { '\0' };
int ans = 0;
for (int j = 0; j < n; j++)
{
strcpy(t, a + (n - j));
strcpy(t + j, a);
for (int temp = n; temp<n+ j; temp++){ //把多余的字符给去掉
t[temp] = '\0';
}
ans = strcmp(t, b);
if (!ans)break;
}
printf("%d\n", !ans);
}
int main()
{
char a[] = { "abefg" };
char b[] = { "bpfga" };
int n = sizeof(a);
comper(a, b, n-1);
return 0;
}
结果如下: