#include <iostream>
#include <string>
std::string encrypt(const std::string& word)
{
std::string result;
if (word.size()) // sanity check for empty string
{
std::size_t i = 0; // index of leftmost character
std::size_t j = word.size() - 1; // index of rightmost character
while (i < j)
{
result += word[i++]; // You get the first character of the word
result += word[j--]; // and then you get the last character
}
if (i == j) // in some cases of words with an odd number of characters, only one character is left!
result += word[i];
}
return result;
}
int main()
{
std::cout << encrypt("hacker") << '\n'; // even number of characters.
std::cout << encrypt("phone") << '\n'; // odd number of characters.
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgoKc3RkOjpzdHJpbmcgZW5jcnlwdChjb25zdCBzdGQ6OnN0cmluZyYgd29yZCkKewogICAgc3RkOjpzdHJpbmcgcmVzdWx0OwoKICAgIGlmICh3b3JkLnNpemUoKSkgICAgLy8gc2FuaXR5IGNoZWNrIGZvciBlbXB0eSBzdHJpbmcKICAgIHsKICAgICAgICBzdGQ6OnNpemVfdCBpID0gMDsgICAgICAgICAgICAgICAgICAvLyBpbmRleCBvZiBsZWZ0bW9zdCBjaGFyYWN0ZXIKICAgICAgICBzdGQ6OnNpemVfdCBqID0gd29yZC5zaXplKCkgLSAxOyAgICAvLyBpbmRleCBvZiByaWdodG1vc3QgY2hhcmFjdGVyCgogICAgICAgIHdoaWxlIChpIDwgaikKICAgICAgICB7CiAgICAgICAgICAgIHJlc3VsdCArPSB3b3JkW2krK107ICAvLyBZb3UgZ2V0IHRoZSBmaXJzdCBjaGFyYWN0ZXIgb2YgdGhlIHdvcmQKICAgICAgICAgICAgcmVzdWx0ICs9IHdvcmRbai0tXTsgIC8vIGFuZCB0aGVuIHlvdSBnZXQgdGhlIGxhc3QgY2hhcmFjdGVyCiAgICAgICAgfQoKICAgICAgICBpZiAoaSA9PSBqKSAgIC8vIGluIHNvbWUgY2FzZXMgb2Ygd29yZHMgd2l0aCBhbiBvZGQgbnVtYmVyIG9mIGNoYXJhY3RlcnMsIG9ubHkgb25lIGNoYXJhY3RlciBp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