字符循環(huán)右移

題目

輸入一個(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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容