1

ユーザーが入力した回転数を使用して単語を暗号化する関数の作成に問題があります。これが私がこれまでに持っているものです:

string encryptWord(string word, int num)
{
  string newWord;
  newWord = word;
  for(int i = 0; i < word.length(); i++)
    {
      newWord[i] = tolower(word[i]);
      if((word[i] >= 'a') && (word[i] <= 'z'))
        {
          newWord[i] = word[i] + (num % 26);
          if(newWord[i] > 'z')
            newWord[i] = newWord[i] - 26;

        }
    }
  return newWord;

}

私がそれをテストするとき、今私のメインで

cout << encryptWord("xyz", 6);

私が得る出力は次のとおりです。de

同様に、復号化のために私は持っています

string decryptRotWord(string word, int num)
{
  string newWord;
  num = num % 26;
  int index;
  for(int i = 0; i < word[i]; i++)
    {
      newWord[i] = tolower(word[i]);
      if(word[i] >= 'a' && word[i] <= 'z')
        {
          index = word[i] - num;
          if(index < 'a')
              index = index + 26;
          newWord[i] = index;
        }
    }
  return newWord;

}

ただし、これについては、テストしても何も出力されません

cout << decryptRotWord("vdds", 2);
4

2 に答える 2

0

あなたの復号化関数では、ループ終了条件に間違いがあると思います:

for(int i = 0; i < word[i]; i++)

暗号化関数と同様に、長さを反復する必要があります

for(int i = 0; i < word.length(); i++)

于 2015-09-24T04:31:56.537 に答える