剑指offer-面试题58_2-左旋转字符串-字符串

/*
题目:
	将字符串的前sep个字符转移到字符串尾部。
*/
/*
思路:
	更好的方法:
		先翻转前sep个字符,再翻转后面的字符,最后全体翻转。
*/
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>

using namespace std;

void verse(char* pBegin,char* pEnd){
    while(pEnd > pBegin){

        char temp = *pBegin;
        *pBegin = *pEnd;
        *pEnd = temp;
        pBegin++;
        pEnd--;
    }
}

char* ReverseSentence(char* pData,int sep){
    char* pBegin = pData;
    char* pEnd = pData;
    int length = 0;
    while(*pEnd != ''){
        pEnd++;
        length++;
    }
    if(sep > length) return nullptr;
    pEnd--;
    verse(pBegin,pBegin+sep-1);
    verse(pBegin+sep,pEnd);
    verse(pBegin,pEnd);

    return pData;

}

int main(){
   char pData[] = "abcdefg";
   char* res = ReverseSentence(pData,2);
   while(*res != ''){
        cout<<*res;
        res++;
   }
}

   

原文地址:https://www.cnblogs.com/buaaZhhx/p/12109595.html