// test.cpp : 定义控制台应用程序的入口点。
//
// 华为面试题
// 给定一个单词字符串, 如: "We are all Welcome you to HuaWei!", 逆序输出此单词字符串, 并保留最后的标点
// 符号不变, 如: "HuaWei to you Welcome all are We!"
//
#include "stdafx.h"
#include <vector>
#include <string>
#include <iostream>
std::vector<std::vector<char>> WordStrSpliter(const std::string& src, char token)
{
if (src.empty()) { return std::vector<std::vector<char>>(); };
std::vector<std::vector<char>> splitList;
std::vector<char> vecChar;
const int Length = src.size();
for (int i = 0; i < Length; ++i)
{
if (src[i] != token)
{
vecChar.push_back(src[i]);
}
else
{
splitList.push_back(vecChar);
vecChar.clear();
}
}
splitList.push_back(vecChar);
return splitList;
}
std::string MyWordStrReverse(const std::string& src, char token = ' ')
{
if (src.empty()) { return std::string(); }
std::vector<std::vector<char>> splitList = WordStrSpliter(src, token);
const int Count = splitList.size();
std::string dest;
for (int i = Count - 1; i > 0; --i)
{
std::string tmp(splitList[i].begin(), splitList[i].end());
dest += tmp + ' ';
}
dest += std::string(splitList[0].begin(), splitList[0].end());
return dest;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::string src = "We are all Welcome you to HuaWei!";
std::string reverse = MyWordStrReverse(src.substr(0, src.length() - 1), ' ');
std::string dest = reverse + src[src.length()-1];
std::cout << "原始字串: " << src << '\n'
<< "逆序输出: " << dest << '\n';
return 0;
}