一个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;
}
程序测试结果如下
不是:
是: