实现右旋移位函数,比如:“abcdefghi”循环右移2位是“hiabcdefg”
void reverse(char *left, char *right)
{
assert(left != NULL);
assert(right != NULL);
while (left < right)
{
char tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
}
void right_loop_move(char *pstr, unsigned short steps)
{
int len = strlen(pstr);
reverse(pstr, pstr + len - steps - 1);
reverse(pstr + len - steps, pstr + len - 1);
reverse(pstr, pstr + len - 1);
}
实现左旋移位函数,比如:“abcdefghi”循环右移2位是“cdefghiab”
void reverse(char *left, char *right)
{
assert(left != NULL);
assert(right != NULL);
while (left < right)
{
char tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
}
void left_loop_move(char *pstr, unsigned short steps)
{
int len = strlen(pstr);
reverse(pstr, pstr + steps - 1);
reverse(pstr + steps, pstr + len - 1);
reverse(pstr, pstr + len - 1);
}