检测两个字符串是近左旋相同还是近右旋相同
#define _CRT_SECURE_NO_WARNINGS 1 #include#include #include #define LEN 33 char *my_fgets(char *str,const int n); int compare_str(const char *str1,const char *str2); int main(){ char str1[LEN]={0},str2[LEN]={0}; while(1){ printf("Input the first string(limit:%d):",LEN-1); if(!my_fgets(str1,LEN)) exit(0); printf("Input the second string(limit:%d):",LEN-1); if(!my_fgets(str2,LEN)) exit(0); int result=compare_str(str1,str2); switch(result){ case 0:printf("字符串"%s"与"%s"毫不相关。n",str1,str2);break; case -1:printf("字符串"%s"与"%s"完全相同。n",str1,str2);break; case 3:printf("字符串"%s"与"%s"近左旋与近右旋皆相同。n",str1,str2);break; case 1:printf("字符串"%s"与"%s"是近左旋相同。n",str1,str2);break; case 2:printf("字符串"%s"与"%s"是近右旋相同。n",str1,str2);break; default:fputs("无法判断。n",stdout);break; } } return 0;} // char *my_fgets(char *str,const int n){ char *gets,*find; if((gets=fgets(str,n,stdin))&&*gets!=10) if(find=strchr(str,10)) *find=0; else while(getchar()!=10); else return 0; return gets;} int compare_str(const char *str1,const char *str2){ int len1=strlen(str1),len2=strlen(str2); if(len1!=len2) return 0; if(!strncmp(str1,str2,len1)) return -1; char *str_copy=(char *)calloc(len1+1,sizeof(char)); char temp[2]={0}; int direction=0; for(int i=0,j=len2-1;i!=j;i++,j--){ if(str1[0]==str2[j]) direction=1; else if(str1[len1-1]==str2[i]) direction=2; if(direction&&abs(i-j)<=1) direction=3; if(direction) break; } strncpy(str_copy,direction==1?str1:str2,len1); for(int i=0;i 另外有一种方法是追加复制自身



