左旋转字符串

问题描述:左旋转字符串。

input:abc 2  output:cab

intput:abcd 2 output :cdab

代码:

 1 #include <iostream>
  2 using namespace std;
  3
  4 void LeftRotateString(char **str,const int m)
  5 {
  6     if(m==0) return;
  7     int length=strlen(*str);
  8     char *ptr=*str;
  9     int i=0,j=0,last=length-1,lastpos=0;
 10     for(;i<m;i++)
 11     {
 12         char temp=ptr[i];
 13         for(j=0;j*m+i+m<length;j++)
 14         {
 15             ptr[j*m+i]=ptr[j*m+i+m];
 16         }
 17         if(i==0) lastpos=j*m;
 18         ptr[j*m+i]=temp;
 19     }
 20     int lastm=m-(length-lastpos);
 21     int start=lastpos-lastm;
 22     if(lastm!=0)
 23     {
 24         ptr=*str+start;
 25         LeftRotateString(&ptr,lastm);
 26     }
 27
 28 }
 29 int main()
 30 {
 31     char str[100];
 32     char *temp=NULL;
 33     int n;
 34     while(1)
 35     {
 36         cin>>str;
 37         temp=str;
 38         cin>>n;
 39         LeftRotateString(&temp,n);
 40         cout<<str<<endl;
 41     }
 42     return 0;
 43 }

Output:

[root@localhost Test]# ./a.out
abc
2
cab
abcd
2
cdab
abcd
3
dabc
abcdefg
4
efgabcd
abc
1
bca

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值