/*字符串旋转 abcedf cbaedf cbafde defabc*/ #include<iostream> #include<cstring> using namespace std; void ReverseString (char *s,int from,int to){//3 5 while(from<to){ //3<5 4==4 char t=s[from]; //t=s[3] cout<<"the t is :"<<t<<endl; s[from++]=s[to]; //s[3]=s[5] cout<<"after reverse,the s[from] is :"<<s[from]<<endl; s[to--]=t; //s[5]=t; cout<<"after reverse,the s[to] is :"<<s[to]<<endl; } } void LeftRotateString(char *s,int n,int m){//三步反转 5 3 m%=n;//若要左移动大于n位,那么与%n是等价的 ReverseString (s,0,m-1); //将X所有字符反转 abc->cba 0 2 cout<<"after the first swap is :"<<s<<endl; ReverseString (s,m,n-1); //将Y所有字符反转 def->fed 3 5 cout<<"after the second swap is :"<<s<<endl; ReverseString (s,0,n-1); //整体反转 0 5 abcdef->defabc cout<<"after the third swap is :"<<s<<endl; } int main(){ char str[10]; cin>>str; int len=strlen(str); int left; cout<<"left :"; cin>>left; cout<<"the lenght of the string is:"<<len<<endl; LeftRotateString(str,len,left); cout<<"after the leftrotate,the string is:"<<str<<endl; return 0; }