定义函数,将字符串循环右移n个字符,例如abcde循环右移两位:deabc
函数接口定义:
void fun(char *str,int n)


定义函数,将字符串循环右移n个字符,例如abcde循环右移两位:deabc
函数接口定义:
void fun(char *str,int n)


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void fun(char *str,int n);
int main()
{
char s[20];
int n;
scanf("%s%d",s,&n);
fun(s, n);
printf("%s", s);
return 0;
}
void fun(char *str,int n)
{
if ((str == NULL) || (n <= 0)) {
return;
}
int nLen = strlen(str);
if (n >= nLen) {
n = n % nLen;
}
char* pHead = (char*)malloc(n * sizeof(char));
char* pTail = (char*)malloc((nLen - n) * sizeof(char));
memset(pHead, 0 ,sizeof(pHead));
memset(pTail, 0, sizeof(pTail));
memcpy(pHead, str + nLen - n, n);
memcpy(pTail, str, nLen - n);
memcpy(str, pHead, n);
memcpy(str + n , pTail, nLen - n);
return;
}