/*1.实现一个函数,可以左旋字符串中的k个字符。
ABCD左旋一个字符得到BCDA
ABCD左旋两个字符得到CDAB */
char * zuo_xuan( char * dest ,int n)
{
char *top = dest;
char *end = dest;
while ( *end !='\0')
{
end++;
}
while (n)
{
*(end+1)=*end;
*(end++)= *(top++);
n--;
}
return top;
}
int main()
{
char a[] = "0";
int num = 0;
printf("请输入你想左旋的字符串\n");
scanf("%s", a);
printf("请输入你想左旋的位数\n");
scanf("%d", &num);
printf("左旋%d位后的字符串为:\n%s\n",num, zuo_xuan(a,num));
//思路:写一个函数参数为左旋的个数
system("pause");
return 0;
}
#include<stdio.h>
#include<string.h>
char * zuo_xuan( char * dest ,int n)
{
char *top = dest;
char *end = dest;
while ( *end !='\0')
{
end++;
}
while (n)
{
*(end+1)=*end;
*(end++)= *(top++);
n--;
}
return top;
}
char * you_xuan( char * dest ,int n)
{
char *top = dest;
char *end = dest;
while ( *end !='\0')
{
end++;
}
while (n)
{
*(--top) = *(--end);
*end = *(end+1);
n--;
}
return top;
}
char c[] = "0";
int pan_duan(char *a, const char * b, int sz1, int sz2)
{
strcpy(c, a);
int n = 0;
if (sz1 != sz2)
{
return 0;
}
for (n = 1; n <= sz1; n++)
{
if (!strcmp(zuo_xuan(a, n), b))
{
return 1;
}
}
for (n = 1; n <= sz1; n++)
{
if (!strcmp(you_xuan(c, n), b))
{
return 1;
}
}
return 0;
}
int main()
{
char a[] = "0";
char b[] = "0";
printf("请输入要比较的两个字符串\n");
scanf("%s %s/n", a, b);
int sz1 = sizeof(a);
int sz2 = sizeof(b);
if (pan_duan(a,b, sz1, sz2))
printf("这两个字符串有左右旋关系\n");
else
printf("这两个字符串无左右旋关系\n");
system("pause");
return 0;
}