//编写函数实现字符串旋转 #include <stdio.h> #include <assert.h> #include <string.h> void reverse(char *left, char *right) { char temp; assert(left); assert(right); while (right > left) { temp = *left; *left = *right; *right = temp; left++; right--; } } char *rotate(char *src, int n) { int len = strlen(src); assert(src); reverse(src, src + n-1); reverse(src + n, src+len-1); reverse(src, src + len - 1); return src; } int main() { char p[] = "abcdef"; printf("%s\n", rotate(p, 2)); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-14 10:56:38