请实现字符串右移位函数,比如:"abcdefghi"循环右移2位就是"hiabcdefg"。
函数原型:void RightLoopMove(char *str, unsigned short steps)
函数参数说明:
pStr: Point to a '\0' terminated string.
steps: The rotate shift numbers.
①暴力移位法:
#include #include #include void RightLoopMove(char *pStr, unsigned short steps)
{
int i = 0;
int len = strlen(pStr);
assert(pStr);
for (i = 0; i < steps; i++)
{
char *pend = pStr + len - 1;
char tmp = *(pStr + len - 1);
while (pStr <= pend)
{
*pend = *(pend - 1);
pend--;
}
*pStr = tmp;
}
}
int main()
{
char str[] = "abcdef";
RightLoopMove(str, 2);
printf("%s\n"