转置密码实现不按预期工作

时间:2018-03-10 20:09:05

标签: c++

我试图实现基本的转置密码,它基本上反转每个单词,同时保持其在字符串中的位置但是当我给它输入“MEET ME TOMORROW”时,它输出“TEEM OMOT EM”而不是“TEEM EM WORROMOT”

@client.command(pass_context=True)
async def pick(ctx):
    author = ctx.message.author.id
    if 1 <= giftRate <= 2:
        if (1 <= lowFishRange <= 20):
            oneto5 = str(5)
            await client.say("<@%s>" % (author) + " recived " + oneto5 + " 
            fish")

        else:
            oneto5 = str(1)
            await client.say("<@%s>" % (author) + " recived " + oneto5 + " 
            fish")
     else:
        await client.say("<@%s>" % (author) + " seemed to have missed the mark, the only way to gurantee fish is by "
                     "waiting for a cool pant cate to give you some.")

2 个答案:

答案 0 :(得分:1)

您的代码有2个问题。首先,

的第二个参数
temp = input.substr(flag, i); 

是子字符串的长度,您应该使用:

temp = input.substr(flag, i-flag);

第二个问题,你没有处理最后一个输入词,为此,我建议你将for条件改为i<=input.length(),以便程序输入if-code with the &#39; \ 0&#39;你的字符串的字符。

答案 1 :(得分:0)

如果您不担心保留空格,可以使用流迭代器获得一些乐趣

#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
#include <algorithm>

int main()
{
    std::istringstream iss("MEET ME TOMORROW");
    std::istream_iterator<std::string> iit(iss);
    std::istream_iterator<std::string> eos;

    std::ostringstream oss;
    std::ostream_iterator<std::string> oit(oss, " ");

    std::transform(iit, eos, oit, [](std::string const &s) { return std::string(s.crbegin(), s.crend()); });
    std::cout << oss.str();

    return 0;
}

https://ideone.com/PlbXfb

TEEM EM WORROMOT