我必须做一个程序,每个单词中的每一个字母都会接受句子并移动n次,但我需要一些帮助。这是我的代码:
#include<iostream>
#include<string>
using namespace std;
string trimSpaces(string trimStr)
{
while (trimStr[0] == ' ')
{
trimStr.erase(0, 1);
}
return trimStr;
}
string getWord(string &sentence)
{
sentence = trimSpaces(sentence);
int i = 0;
while (sentence[i] != ' ' && i < sentence.size())
{
i++;
}
string word = sentence.substr(0, i);
sentence.erase(0, i);
return word;
}
string transformWord(string word, int n)
{
//char* resultWord = new char[word.length()]();
string resultWord;
string helpingWord;
char firstSymbol = word[0];
char secondSymbol = word[1];
char preLastSymbol = word[word.length() - 2];
char lastSymbol = word[word.length() - 1];
bool fl = false;
bool fl2 = false;
bool fl3 = false;
if (firstSymbol == '"')
{
fl2 = true;
firstSymbol = word[1];
secondSymbol = word[2];
}
if (lastSymbol == '.' || lastSymbol == '"' || lastSymbol == ',' || (preLastSymbol == '.' && lastSymbol == '"'))
{
if (preLastSymbol == '.' && lastSymbol == '"')
{
fl = true;
word.erase(word.length() - 3, 2);
}
else
{
fl3 = true;
word.erase(word.length() - 2, 1);
}
}
for (int i = 0; i < word.length(); i++)
{
resultWord += word[(i + n % word.length()) % word.length()];
}
helpingWord = resultWord;
if (fl)
{
helpingWord += (preLastSymbol + lastSymbol);
}
if (fl2)
{
helpingWord = word[0] + helpingWord;
}
if (fl3)
{
helpingWord += lastSymbol;
}
return helpingWord;
}
int main()
{
string sentence;
string resultSentence;
int n;
getline(cin, sentence);
cin >> n;
while (sentence.size() > 0)
{
string word = getWord(sentence);
word = transformWord(word, n);
resultSentence += word;
}
cout << resultSentence << '\n';
}
我认为问题来自这里
for (int i = 0; i < word.length(); i++)
{
resultWord += word[(i + n % word.length()) % word.length()];
}
当我将word
的字母添加到resultWord
时
如果我们输入:Pesho 3
,则输出必须为shoPe
,但我的输出为hoPes
;
如果我们输入如下句子:
Oh, what fun it is to
ride in a one horse open sleigh.
结果必须是:hO, hatw fun ti si ot ider ni a one rseho peno ighsle.
答案 0 :(得分:0)
这种残酷而愚蠢的方式。这不是超级快速或高效,但它真的,非常容易编写和阅读。经验法则是(在这里解释爱因斯坦)使一切尽可能简单,但并不简单。如果这还不够快,那么有足够的时间来调整它。但如果你现在无用地调整它,你可能会浪费宝贵的时间来优化不需要更快的东西。
警告:
这是标点符号盲,但我已经在你可以处理标点符号的地方插入了注释。简单的黑客是:当最后一个字符是标点符号时,将其移动到堆栈。旋转字符串的剩余部分,然后从堆栈弹出到旋转字符串的末尾。
现在进入旋转!
#include<iostream>
#include<string>
#include <sstream>
#include <algorithm>
int main()
{
// hard coding the input for ease of test
std::string sentence = "Oh, what fun it is to\nride in a one horse open sleigh. ";
std::stringstream resultSentence;
// getline(cin, sentence); don't need this for test. Hard-coded the input
// put sentence in stream for easy parsing
std::stringstream stream(sentence);
std::string word;
// chop stream into words. Note this is punctuation-blind. You will have
// to be a bit smarter.
while (stream >> word)
{
// shift left one character
// trim and store trailing punctuation here
std::rotate(word.begin(), word.begin()+1, word.end());
// add to output. Note the redundant space that will be added to the end
// it may become important.
resultSentence << word << /* add trailing punctuation here */" ";
}
// print result.
std::cout << resultSentence.str() << '\n';
}