判斷一個字符串是否是另一個字符串的左旋,如果是輸出左旋的次數

一個c語言程序實現判斷一個字符串是否是另一個字符串的左旋,代碼如下:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<windows.h>
#define num 100000
int  check_levo(char str1[num], char str2[num])
{
    int i = 0;
    int j = 0;
    int len = strlen(str1);
    for (j = 1; j <= len; j++)
    {
        int tmp = str1[0];
        for (i = 0; i < len - 1; i++)
        {
            str1[i] = str1[i + 1];
        }
        str1[len - 1] = tmp;
        if (strcmp(str1,str2)==0)
        {
            return j;
        }
    }
    return 0;
}
int main()
{
    char str1[num] = { 0 };
    char str2[num] = { 0 };
    int times = 0;
    printf("please enter the str1 :");
    gets(str1);
    printf("please enter the str2 :");
    gets(str2);
    if (times=check_levo(str1, str2))
    {
        printf("str2 is the levorotation of str1,and str2 is the %dth of levorotation\n",times);
    }
    else
    {
        printf("str2 isn't the levorotation of str1!\n");
    }
    system("pause");
    return 0;
}

程序測試結果如下
不是:
這裏寫圖片描述
是:
這裏寫圖片描述

發佈了31 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章