★判斷一個字符串是否為另外一個字符串旋轉之后的字符串。
例如:給定s1 = AABCD和s2 = BCDAA,返回1,給定s1=abcd和s2=ACBD,返回0.
AABCD左旋一個字符得到ABCDA ? ? AABCD右旋一個字符得到DAABC? ? ? ? ? ? ? ? ??
AABCD左旋兩個字符得到BCDAA ? ? AABCD右旋兩個字符得到CDAAB? ? ? ? ? ? ? ? ?
#include?<stdio.h>
#include?<string.h>
#include?<assert.h>
#include<stdlib.h>
int?spin(char?*p,?char?*q)
{
assert(p?!=?NULL?&&?q?!=?NULL);???//斷言傳入的指針為不為空,用以拋出異常
strncat(p,?p,?strlen(p));?????//strncat函數用以連接兩個字符串,若用strcat則可能會發生越界,數組長度不夠容納越界處后續的字符,strlen用以求出字符串的長度
if?(strstr(p,?q)?==?NULL)??????//strstr用以在原字符串中查找目標字符串,注意參數的位置順序
{
return?0;
}
else
{
return?1;
}
}
int?main()
{
char?p[20]?=?"AABCD";??????????//原字符串
char?*q?=?"BCDAA";?????????????//目標字符串char?r[20]?=?"abcd";??????????//原字符串char?*s?=?"ACBD";????????????//目標字符串printf("字符串:%s\t%s\n",p,q);
int?ret?=?spin(p,?q);
printf("BACK_VALUE=%d\n",ret);????//輸出返回值
printf("字符串:%s\t%s\n",r,s);
int?rew?=?spin(r,?s);
printf("BACK_VALUE=%d\n",rew);????//輸出返回值
system("pause");
return?0;
}
轉載于:https://blog.51cto.com/10738469/1709162