字符串循环右移n位

来源:互联网 发布:mysql truncate所有表 编辑:程序博客网 时间:2024/06/05 04:35


void loopMove(char* str,int n)//使字符串循环右移n位{int i = 0;char *temp = NULL;int strLen = 0;char *head = str;//指向字符串头while (*str++);strLen = str-head-1;//计算字符串长度n = n%strLen;//计算字符串尾部移到头部的字符个数temp = (char*)malloc(n);//分配内存for (i = 0;i<n;i++){temp[i] = head[strLen-n+i];//临时存放尾部移到头部的字符}for (i = strLen-1;i>=n;i--){head[i] = head[i-n];}for (i=0;i<n;i++){head[i] = temp[i];//从临时内存区复制尾部字符}free(temp);}


原创粉丝点击