#include <stdio.h>
#define MAXLINE 4096
void moven(char *to, const char *from, const int n)
{
const char *tmp = from;
for (; *(tmp + n) != '\0'; tmp++);
for (; (*to = *tmp) != '\0'; to++, tmp++);
for (; *(from + n) != '\0'; from++) {
*to++ = *from;
}
*to++ = '\0';
}
int main(int argc, char *argv[])
{
char result[MAXLINE];
moven(result, argv[1], 2);
printf("result = %s\n", result);
return 0;
}
运行结果如下:
cheny@cheny-laptop:~/string$ gcc -o moven moven.c
cheny@cheny-laptop:~/string$ ./moven aabbcc
result = ccaabb
cheny@cheny-laptop:~/string$ ./moven ccbbaa
result = aaccbb
这种面试题也比较典型,给定字符串,然后要向右移动n位,最右边的n位移动到字符串的最左边n位,写的不好请大家指正,谢谢!