C语言对字符串循坏左移m次

代码:

#include <stdio.h>
#include <string.h>

#define N 1024

char* fun(char* str, int m)
{
    int totalLength = strlen(str);
    static char ret[N];
    memset(ret, 0, N);

    memcpy(ret, str+m, totalLength-m);
    memcpy(ret+(totalLength-m), str, m);

    return ret;
}

int main(int argc, char* argv[])
{
    char inputStr[N];
    memset(inputStr, 0, N);
    int moveCount = 0;
    puts("please input string and move count:");
    scanf("%s %d", inputStr, &moveCount);
    puts(fun(inputStr, moveCount));

    return 0;
}

 

posted @ 2021-07-17 14:01  朱小勇  阅读(92)  评论(0编辑  收藏  举报