移动字母 c语言,字母n次移动字母

我必须做一个程序,每个字母每个字母移动n次,每次移动n次,但是我需要一些帮助。这里是我的代码:字母n次移动字母

#include

#include

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到​​ 的字母如果我们输入: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

什么,_exactly_,是你的问题?你需要什么样的_help_? –

+1

将字符串拆分为单词,'std :: rotate'每个单词,然后将它们拼接在一起。 –

+0

你甚至可以在原始字符串(或其副本)中旋转,因为std :: rotate在迭代器上操作(因此它只能旋转字符串的一部分)。 –

你可能感兴趣的:(移动字母,c语言)