gpt4 book ai didi

C++使用备用字母加密句子(字符数组)(可能重复)

转载 作者:太空宇宙 更新时间:2023-11-04 12:49:20 25 4
gpt4 key购买 nike

我在处理项目的一部分时遇到了一些麻烦。我一直在弄乱代码,但我无法让它工作。基本上,该项目执行以下操作:

  1. 输入一个词
  2. 删除重复项
  3. 将修改后的单词存储到备用字母表中
  4. 使用那个替代字母来加密一个句子

我卡在数字 4 上了。

这是我的代码:

#include <iostream>
#include <string>

using namespace std;

void fixRepeats(char array[], int size); //Function to remove duplicates
void fixSpaces(char array[], int size); //Function to remove spaces
void copy(char array[], char array2[], int size);

int main()
{
char word[25];
char modWord[25]; //User inputted word without repeated letters
char regAlpha[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char altAlpha[27]; //Alternate alphabet created from the word
char sentence[100];
int lenWord;
int lenModWord;
int lenAlpha = 27;
int lenSent;

cout << "Please enter a single word: "; //Get user input

cin >> word;

lenWord = strlen(word); //Get length of word

fixRepeats(word, lenWord); //Function call from main
fixSpaces(word, lenWord); //Function call from main

lenModWord = strlen(word); //Get length of the modified word

copy(word, modWord, lenModWord); //Function call from main
copy(modWord, altAlpha, lenModWord); //Function call from main

for (int i = 0; i < lenAlpha; i++)
{
for (int j = 0; j < lenModWord; j++)
{
if (regAlpha[i] == altAlpha[j])
{
regAlpha[i++];
}
}

altAlpha[lenModWord++] = regAlpha[i];
}

fixRepeats(altAlpha, lenAlpha);
fixSpaces(altAlpha, lenAlpha);

cout << "\nModified Word: " << word << endl;
cout << "\nAlphabet------: " << regAlpha << endl;
cout << "Encryption Key: " << altAlpha << endl;

cin.ignore(100, '\n');

cout << "\nPlease enter a sentence: "; //Get user input
cin.get(sentence, 99, '\n');

lenSent = strlen(sentence);

//-------------------------------#4------------------------------------

for (int i = 0; i < lenAlpha; i++)
{
for (int j = 0; j < lenSent; j++)
{
if (altAlpha[i] == sentence[j])
{
sentence[j] = regAlpha[i];
}
}
}

//-------------------------------#4------------------------------------

cout << "\nEncrypted message: " << sentence << endl;

cout << endl;

return 0;
}

void copy(char array[], char array2[], int size)
{
for (int i = 0; i < size; i++)
{
array2[i] = array[i];
}
}

void fixRepeats(char array[], int size)
{
//Use nested loop to check each character
for (int i = 0; i <= size; i++)
{
for (int j = i + 1; j <= size; j++)
{
if (array[i] == array[j]) //Check for duplicates
{
array[j] = '\0'; //Change duplicate to a null char
}
}
}
}

void fixSpaces(char array[], int size)
{
//Use nested loop to check each character
for (int i = 0; i < size; i++)
{
if (array[i] == '\0') //Check for space or null char
{
for (int j = i + 1; j < size; j++)
{
if (array[j] != '\0') //Check the next position
{
swap(array[j], array[i]); //Swap positions
break;
}
}
}
}
}

我在代码中用破折号和 #4 标记了我的问题。

期望的结果是这样的:

Please enter a single word: HELLO

Modified Word: HELO

Alphabet------: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Encryption Key: HELOABCDFGIJKMNPQRSTUVWXYZ

Please enter a sentence: HE IS HUMAN

Encrypted message: DA FS DUKHM

另一个使用相同词的例子是:

Please enter a sentence: COMPUTER SCIENCE

Encrypted message: LNKPUTAR SLFAMLA

Please enter a sentence: PLEASE HELP

Encrypted message: PJAHSA DAJP

当我运行我的代码时,它会变成这样:

Please enter a single word: HELLO

Modified Word: HELO

Alphabet------: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Encryption Key: HELOABCDFGIJKMNPQRSTUVWXYZ

Please enter a sentence: HE IS HUMAN

Encrypted message: EO OS EUOEO

和:

Please enter a sentence: COMPUTER SCIENCE

Encrypted message: LHOPUTOR SLOOOLO

Please enter a sentence: PLEASE HELP

Encrypted message: PLOESO EOLP

其他一切都很好,只是加密部分出了问题。我也知道这是完全错误的(这更像是一次尝试)。

关于如何修复该单个部分的任何建议(假设它可以用于输入的任何单词)?

谢谢。对于冗长的帖子,我们深表歉意。

最佳答案

您正在使用目标字母表进行检查,然后使用错误的索引和源字母表进行修改。

这个:

    if (altAlpha[i] == sentence[j])
{
sentence[i] = regAlpha[i];
}

应该是:

    if (regAlpha[i] == sentence[j])
{
sentence[j] = altAlpha[i];
break;
}

中断是必要的,以避免多次加密某些字符。


您还需要更改循环的顺序以避免多次加密某些字符。所以这个:

for (int i = 0; i < lenAlpha; i++)
{
for (int j = 0; j < lenSent; j++)

变成:

for (int j = 0; j < lenSent; j++)
{
for (int i = 0; i < lenAlpha; i++)

将它们拼接在一起,您的 for 循环应该如下所示:

for (int j = 0; j < lenSent; j++)
{
for (int i = 0; i < lenAlpha; i++)
{
if (regAlpha[i] == sentence[j])
{
sentence[j] = altAlpha[i];
break;
}
}
}

关于C++使用备用字母加密句子(字符数组)(可能重复),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49808097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com