【发布时间】:2017-07-02 09:05:42
【问题描述】:
我需要在一个文件中的 4 个不同的句子上使用 getline(infile, aSentence) 并将它们存储为字符串。然后,我必须创建一个算法,将每个单词的第一个字母移动到最后一个字母,然后在单词后面加上“ay”。
例如:“you may call me claptrap”会变成“ouyay aymay allcay emay laptrapcay”
最好的方法是什么?我正在考虑使用aSentence.find(" ") 作为空白,aSentence.append 添加“ay”。我不知道如何移动字母位置。
希望这是有道理的,谢谢。
到目前为止我的代码(不完整,但这是概念):
int characterIndex = 0;
char firstChar = sentence.at(characterIndex);
char currentChar = sentence.at(characterIndex);
while (currentChar != '.');
{
if(currentChar == ' ')
{
sentence.replace(characterIndex, "ay")
}
}
【问题讨论】:
-
用什么语言?还是您在寻找伪代码?
-
如果您尝试过一些代码,请将其放在问题中,这样会更容易获得帮助!
-
对不起,在 C++ 中。这是我到目前为止的部分伪代码,但它是概念。
-
单词之间只用空格分隔?
-
如果你的句子没有
'.',为什么还要搜索呢?贴一个输入文件的例子
标签: c++ string replace find append