題目
輸入一個(gè)字符串,每個(gè)字符循環(huán)向右移動(dòng)一位,判斷移動(dòng)后能否與所輸入的第二個(gè)字符串相匹配
輸入說明
anhui
huian
輸出說明
Yes
總結(jié)
當(dāng)時(shí)用的while循環(huán),導(dǎo)致不好結(jié)束,其實(shí)長(zhǎng)為len的字符串最多經(jīng)過len次就會(huì)恢復(fù)到最初狀態(tài),也就是說用for循環(huán)len次就可以了
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char s1[1000];
char s2[1000];
int i;
int j;
gets(s1);
gets(s2);
int len1 = strlen(s1);
int len2 = strlen(s2);
//最多循環(huán)len次,恢復(fù)原來狀態(tài)
for(i=0; i<len1; i++)
{
char tmp = s1[len1-1];
for(j=len1-2; j>=0; j--)
s1[j+1] = s1[j];
s1[0] = tmp;
if(strcmp(s1,s2)==0)
{
printf("Yes\n");
break;
}
else
continue;
}
if(strcmp(s1,s2)!=0)
printf("No\n");
return 0;
}