字符串的旋转

把字符串”abcdef”转换为”defabc”,行话:把字符串的前n个字符放在字符串的后面。
思路:

  • 首先,把字符串的前n个字符先对调,即:abc->cba
  • 然后,把剩余的字符串对调,即:def->fed
  • 最后,把整个字符串都对调,即:cbafed->defabc
#include "iostream"
#include "string"

using namespace std;
void shiftString(string& str,int start , int end)
{
    while(start<end)
    {
        char c;
        c=str[start];
        str[start]=str[end];
        str[end]=c;
        start++;
        end--;
    }
}

void reverseString(string& str, int shiftNum , int strNum)
{
    if(shiftNum<strNum)
    {
        shiftString(str,0,shiftNum-1);
        shiftString(str,shiftNum,strNum-1);
        shiftString(str,0,strNum-1);
    }
}
int main()
{
    string str="abcdef";
    reverseString(str);
    cout<<str<<endl;
    //Good luck!
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值