用于加密/解密段落的Caesar Cipher程序

时间:2018-02-16 14:09:43

标签: c caesar-cipher

首先,我是编程的新手,所以要温柔。我也一直在做这项任务无济于事。该任务是创建一个Caesar Cipher程序,用于加密或解密最多100个字符的段落。它实际上是两个独立的实验室。第一个实验室是加密,然后第二个实验室是解密。一旦我弄清楚如何创建加密程序,解密程序应该很简单,因为我只能进行语义更改来解密而不是加密。无论如何这里是我到目前为止的代码。它通过了他们给我们的5个测试中的4个,但由于某种原因,有一个测试,其中最后一个字符是'@'符号。这对我没有任何意义,因为它在任何其他测试中都没有发生,并且我相信我的代码在字符串的位置放置一个'\ 0'符号,因此'@'不应该出现在这一个测试中。< / p>

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

// ---------------------- DO NOT MODIFY THIS SECTION -----------------------    ---------
#define MAX_PGRAPH_LENGTH 100
#define MAX_WORD_LENGHT 20

int main(void) {   
// definitions
char plaintext[MAX_PGRAPH_LENGTH] = "";
char ciphertext[MAX_PGRAPH_LENGTH];
char input[MAX_WORD_LENGHT];    

// read the key
int key;
scanf("Key: %d, ", &key);

// read text
scanf("Input: ");
while (true)
{
    scanf("%s", input);
    if (strlen(plaintext) + strlen(input) + 1 > MAX_PGRAPH_LENGTH)
        break;

    strcat(plaintext, input);
    strcat(plaintext, " ");
}
plaintext[strlen(plaintext) - 1] = '\0';
// ---------------------- --------------------------------------------------    ---------




int i;

for(i = 0; i < strlen(plaintext); ++i) 
{

  if(plaintext[i] >= 'a' && plaintext[i] <= 'z') {
     ciphertext[i] = ((plaintext[i] + (key % 26) - 97) % 26) + 97; 

     if(ciphertext[i] > 'z') {
        ciphertext[i] = ((plaintext[i] + (key % 26) - 97) % 26) + 97 - 26; }
  }


  else if(plaintext[i] >= 'A' && plaintext[i] <= 'Z') {
     ciphertext[i] = ((plaintext[i] + (key % 26) - 'A') % 26) + 'A';

     if(ciphertext[i] > 'Z') {
        ciphertext[i] = ((plaintext[i] + (key % 26) - 'A') % 26) + 'A' - 26; }
  }

  else {
     ciphertext[i] = plaintext[i]; }

}
 for(i = 0; i < strlen(plaintext) && plaintext[i] == '\0'; ++i) {
  ciphertext[i] = '\0'; }



// ---------------------- DO NOT MODIFY THIS SECTION -----------------------    ---------
printf("   Key: %d\n", key);
printf(" Input: %s\n", plaintext); 
printf("Output: %s\n", ciphertext);
// ---------------------- --------------------------------------------------    ---------
}

正如您所看到的,实验室为我们提供了设置和多个代码块,我负责编码的实际Caesar密码部分。我的问题是,我没有在C的最后位置正确设置'\ 0'吗?我注意到的一件事是,如果我输入诸如“狗跑”之类的输入,当输入被打印到屏幕上时,它将打印出“狗跑跑跑了跑跑了......”直到它达到100个字符。幸运的是,他们给我们运行的所有测试都是超过100个字符的段落,所以我实际上不必担心通过我加密少于100个字符的测试。但我仍然想知道为什么我的明文字符串一遍又一遍地重复最后一个输入字。对于这篇荒谬的长篇文章感到抱歉,我已经尝试了一切,但不知道我哪里出错了。

2 个答案:

答案 0 :(得分:1)

它重复了最后一个字,因为你没有退出无限循环。

break

退出循环的唯一方法是执行input语句,这只能使plaintextMAX_PGRAPH_LENGTH的总长度大于scanf()。只重复最后一个单词的原因是因为很可能df.set_index([df.index % 3, df.index // 3])\ .unstack()\ .sort_index(level=1, axis=1)\ .set_axis(list('abcdefgh'), axis=1, inplace=False) 逐字读取输入。

另外,你没有用@字符传递测试用例的原因可能是因为字母字符以十进制65开头(对应于A),而@的十进制值是64. A之间的区别并且@是1并且代码在@测试用例中失败并不是我认为的巧合。所以,我认为你应该仔细检查算术运算并跟踪代码输入。

答案 1 :(得分:0)

重复在提供的代码中......不在您的代码中。

读取下一个单词(永远),直到达到最大段落长度。 如果没有下一个单词,则输入的值不会更新,只需重复一次,直到达到while (true) { scanf("%s", input); if (strlen(plaintext) + strlen(input) + 1 > MAX_PGRAPH_LENGTH) break; strcat(plaintext, input); strcat(plaintext, " "); } 的总长度

{{1}}