我试图实现基本的转置密码,它基本上反转每个单词,同时保持其在字符串中的位置但是当我给它输入“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.")
答案 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;
}
TEEM EM WORROMOT