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