实现字符串的循环移位,如abcdefg左移两位为cdefgab;
#include "string"
#include "iostream"
using namespace std;
void Reversebuffer(char buffer[],int start, int end )
{
char temp;
while(start<end)
{
temp=buffer[start];
buffer[start++]=buffer[end];
buffer[end--]=temp;
}
}
int main()
{
char str[] = "abcdefg";
cout << str << endl;
cout<<"循环移位k的大小为:";
int k = getchar();//返回字符的Ascall码
k=k-48;
cout<<k<<endl;
if (k<0)
{
cout<<"输入错误的字符";
}
int len=strlen(str);
int n=k%len;
int m=len-n;
Reversebuffer(str,0,len-1);
Reversebuffer(str,0,m-1);
Reversebuffer(str,m,len-1);
cout << str << endl;
return 0;
}
字符串循环移位
最新推荐文章于 2022-04-08 21:40:17 发布