(程序员面试题)字符串处理之循环右移

来源:互联网 发布:杨他他淘宝客 编辑:程序博客网 时间:2024/06/13 09:55
#include <stdio.h>#define MAXLINE 4096void 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位,写的不好请大家指正,谢谢!



原创粉丝点击