// revertString.cpp : 定义控制台应用程序的入口点。 // #include <string.h> #include <stdlib.h> #include <stdio.h> //字符串逆转函数 void revertString(char* pStart,char* pEnd){ if(pStart!=NULL&&pEnd!=NULL){ while(pStart<=pEnd){ char tmp=*pStart; *pStart=*pEnd; *pEnd=tmp; pStart++; pEnd--; } } } //左旋转元素,从0至n,到字符串的尾部 char* leftRotateString(char* pStr,unsigned int n){ if(pStr){ int length=static_cast<int>(strlen(pStr)); if(length>0 && n>=0){ char* pFirstStart=pStr;//起始旋转 char* pFirstEnd=pStr+n-1;//旋转结束 char* pSecondStart=pStr+n;//不动开始 char* pSecondEnd=pStr+length-1;//不动结束 revertString(pFirstStart,pFirstEnd);//旋转部分 revertString(pSecondStart,pSecondEnd);//不动部分 revertString(pFirstStart,pSecondEnd);//整体旋转 } } return pStr; } int main(int argc, char* argv[]) { char str[]="iloveu"; printf("%s/n",leftRotateString(str,2)); system("pause"); return 1; }