欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C语言:判断一个字符串是否为另外一个字符串旋转之后的字符串。

程序员文章站 2022-05-09 20:39:33
#define _CRT_SECURE_NO_WARNINGS 1 #include #include v...
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <string.h>
void reserve(char *left, char*right)
{
 while (left < right)
 {
  char tmp = *left;
  *left = *right;
  *right = tmp;
  left++;
  right--;
 }
}
void left_reserve(char arr[], int k)
{
 int len = strlen(arr) - 1;
 char *pstart = &arr[0];
 char *pend = arr + len;
 reserve(pstart, pstart + k - 1);
 reserve(pstart + k, pend);
 reserve(pstart, pend);
}
void right_reserve(char arr[], int k)
{
 int len = strlen(arr) - 1;
 char *pstart = &arr[0];
 char *pend = arr + len;
 reserve(pstart, pstart + len - k);
 reserve(pend - k+1, pend);
 reserve(pstart, pend);
}
int main()
{
 char arr[10] = "AABCD";
 char arr2[10] = "ABCDA";
 char *p = arr;
 int k = 5;
 while (k)
 {
  left_reserve(arr, k);
  if (strcmp(arr, arr2) == 0)
  {
   printf("1\n");
   break;
  }
  right_reserve(p, k);
  if (strcmp(p, arr2) == 0)
  {
   printf("1\n");
   break;
  }
  k--;
 }
 if (0 == k)
 {
  printf("0\n");
 }
 system("pause");
}