#include <cstdio>
#include <cstring>
void rotate(char *start, char *end)
{
while(start != NULL && end !=NULL && start<end)
{
char temp=*start;
*start=*end;
*end=temp;
start++;
end--;
}
}
void left_right_rotate(char *p,int m)
{
if(p==NULL)
return ;
int len=strlen(p);
char *xfirst,*xend;
char *yfirst,*yend;
if(m>0)
{
m =m%len;
xfirst=p;
xend=p+m-1;
yfirst=p+m;
yend=p+len-1;
rotate(xfirst,xend);
rotate(yfirst,yend);
rotate(p,p+len-1);
}
else if (m==0) return;
else
{
m=(-1)*m;
m= m%len;
m=len-m;
xfirst=p;
xend=p+m-1;
yfirst=p+m;
yend=p+len-1;
rotate(xfirst,xend);
rotate(yfirst,yend);
rotate(p,p+len-1);
}
}
int main(void)
{
char str[]="abcdefghij";
printf("%s\n",str);
left_right_rotate(str,-3);
printf("%s\n",str);
return 0;
}
指针-字符处理-字符旋转
最新推荐文章于 2024-06-21 12:38:50 发布