首页 > 其他 > 详细

剑指offer-面试题58_1-翻转单词顺序-字符串

时间:2019-12-27 20:59:09      阅读:79      评论:0      收藏:0      [点我收藏+]
/*
题目:
	输入一个英文句子,翻转单词顺序,但单词内部顺序不变。
*/
/*
思路:
	先翻转整个句子,再将每个单词分别翻转一次。
*/
#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){
    char* pBegin = pData;
    char* pEnd = pData;
    while(*pEnd != ‘\0‘){
        pEnd++;
    }
    pEnd--;
    verse(pBegin,pEnd);
    char* res = pData;


    pBegin = pEnd = pData;
    while(*pBegin != ‘\0‘){
        if(*pBegin == ‘ ‘){
            pBegin++;
            pEnd++;
        }else if(*pEnd == ‘ ‘ || *pEnd == ‘\0‘){
            verse(pBegin,--pEnd);
            pBegin = ++pEnd;
        }else{
            pEnd++;
        }
    }
    return pData;

}

int main(){
   char pData[] = "I am a student.";
   char* res = ReverseSentence(pData);
   while(*res != ‘\0‘){
        cout<<*res;
        res++;
   }
}

  

剑指offer-面试题58_1-翻转单词顺序-字符串

原文:https://www.cnblogs.com/buaaZhhx/p/12109528.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!