字符串循环左移

来源:互联网 发布:linux jps命令 编辑:程序博客网 时间:2024/04/29 05:13

我的实现思路主要是用 模运算 实现 ,这样一次遍历即可 代码如下:

#include <stdio.h>  #include <stdlib.h>  #include <string.h>  char* move_string_left(char *str, int mov)  {    int n = strlen(str);if(0 == mov%n)return str;char *result =(char *)malloc(sizeof(char)*n);for(int i = mov;i<n+mov;++i){    result[i-mov] = str[i%n];}return result;}int main(int arc,char *arcv[]){   char * temp ="abcdefghijklmnopq";    char * result = move_string_left(temp,10);   printf("%s\n",result); return 0; }