定义函数,将字符串循环右移n个字符,例如abcde循环右移两位:deabc
函数接口定义:
void fun(char *str,int n)
关于#c++#的问题:将字符串循环右移n个字符,例如abcde循环右移两位
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- sinJack 2022-05-27 12:39关注
#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; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用