在单词中移动字母n次

Move letter in word n-times

本文关键字:单词中 移动      更新时间:2023-10-16

我必须做一个程序,每个单词中的每个字母都取句子并移动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文档