在单词中移动字母n次
Move letter in word n-times
我必须做一个程序,每个单词中的每个字母都取句子并移动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.
残酷而愚蠢的方式。这不是超级快速或高效,但它真的非常非常容易编写和阅读。经验法则是,(在这里解释爱因斯坦)让一切尽可能简单,但不要更简单。如果这还不够快,请有足够的时间稍后进行调整。但是,如果你现在无用地调整它,你可能会浪费宝贵的时间来优化不需要更快的东西。
警告:
这是标点盲,但我插入了关于您可以在哪里处理标点符号的注释。简单的技巧是:当最后一个字符是标点符号时,将其移动到堆栈中。旋转字符串的其余部分,然后将堆栈弹出到旋转字符串的末尾。
现在开始轮换!
#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 tonride 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';
}
std::stringstream
文档
std::rotate
文档
相关文章:
- 如何在C++中移动字母表中的字母?
- 从其他容器中移动构造"std::map"
- 当您在此单词中搜索单词时调整字符数组的大小?
- 计算单词中的音节数,但考虑到单词中彼此相邻的任何元音都算作一个音节
- 使用 C++在 OpenGL 中移动自动旋转的 3D 多边形
- 是否可以在C++中移动临时对象的属性?
- 如何使用Qt删除单词中的表行
- 在同一容器中移动元素
- 如何在相对于观看方向的地形中移动
- C++ - 使用结构数据类型将单词中的单个小写字符更改为大写,反之亦然
- 如何在QGraphicsView中移动QWidget?
- 将外部库"inside"项目中移动
- 在 C++11 中移动语义
- 复制省略并在返回值中移动语义
- C++中移动赋值运算符的继承
- Qt:如何让QGraphicsPixmapItem自动在重复路由中移动
- 计算单词中的字母
- 在预分配的内存中移动数据
- 在 C++ 中移动数组元素
- 在单词中移动字母n次