标签:c;旋转字符串
/************************************************************************************ 4.判断一个字符串是否为另外一个字符串旋转之后的字符串。 例如:给定s1 = AABCD和s2 = BCDAA,返回1, 给定s1=abcd和s2=ACBD,返回0. AABCD左旋一个字符得到ABCDA AABCD左旋两个字符得到BCDAA AABCD右旋一个字符得到DAABC AABCD右旋两个字符得到CDAAB ************************************************************************************/ #include <stdio.h> #include<assert.h> #include<string.h> int is_rotate_num(const char *aim,char *source) { char *pstart = source; char *pend = source; int i = 0,j=0; assert(aim&&source); if (strlen(aim) != strlen(source))//字符串长度不等 肯定不是 return 0; while (*(pend)) { char ch = *pstart; while (*(pstart + 1)) { *pstart = *(pstart + 1); pstart++; } *pstart = ch; pstart = source; if (strcmp(aim, source) == 0) return 1; pend++; } return 0; } int main() { char arr[] = "我是帅哥!"; printf("%d\n", is_rotate_num("!我是帅哥",arr ));// 0 不是 1 是 printf("%s\n",arr);//输出旋转后的字符串 对比 return 0; }
标签:c;旋转字符串
原文地址:http://shaungqiran.blog.51cto.com/10532904/1683112