CS50凯撒密码输出错误(pset2) [英] CS50 Caesar Cipher giving incorrect output (pset2)

查看:143
本文介绍了CS50凯撒密码输出错误(pset2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个程序,该程序使用Caesar的密码对邮件进行加密。我的代码中存在某种逻辑错误,并且我一直在努力寻找它。逻辑对我来说很有意义,但是我一直得到错误的输出。

I am trying to implement a program that encrypts messages using Caesar's cipher. I have some sort of logical error in my code, and I've been pulling my hair trying to find it. The logic makes sense to me, but I keep getting the wrong output. Please can someone guide me with this issue!

#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, string argv[])
{
    //Check that program was run with one command-line argument
    if (argc == 2)
    {
       int n = strlen(argv[1]);

       //Iterate over the provided argument to make sure all characters are digits

       for (int i = 0; i < n; i++) {

           if (isdigit(argv[1][i])) {
               //Convert that command-line argument from a string to an int
             //  int key = atoi(argv[1]);
                   int key = atoi(argv[1]);
                //Prompt user for plaintext
                string plaintext = get_string("Enter plain text: ");
                printf("ciphertext: ");

                //int l = strlen(plaintext);
                //Iterate over each character of the plaintext:
                for (int j = 0, l=strlen(plaintext); j < l; j++) {

                if (isalpha(plaintext[j])) {

                    if (isupper(plaintext[j])) {

                         printf("%c", (((plaintext[i] - 65) + key) % 26) + 65);
                    }
                    if (islower(plaintext[j])) {
                         printf("%c", (((plaintext[i] - 97) + key) % 26) + 97);
                    }

                  }
                  else
                    {
                         printf("%c", plaintext[i] );
                    }
                }
                printf("\n");
                return 0;
           }
           else {
                printf("Usage: ./caesar key\n");
                return 1;
           }
       }
    }
    else
    {
         printf("Usage: ./caesar key\n");
         return 1;
    }

}


推荐答案

执行加密的代码不应位于检查 argv [1] 中的所有字符均为数字的循环内。首先执行验证密钥的循环。如果成功,则请求纯文本并执行加密。

The code that performs the encryption should not be inside the loop that checks that all the characters in argv[1] are digits. First do the loop that validates the key. If that succeeds, then ask the the plaintext and perform the encryption.

主要的逻辑错误是您拥有 plaintext [i] 在许多地方。那应该是 plaintext [j]

The major logical error is that you have plaintext[i] in a number of places. That should be plaintext[j].

#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, string argv[])
{
    //Check that program was run with one command-line argument
    if (argc == 2)
    {
        int n = strlen(argv[1]);

        //Iterate over the provided argument to make sure all characters are digits
        for (int i = 0; i < n; i++) {
            if (!isdigit(argv[1][i])) {
                printf("Error: Key must be numeric");
                return 1;
            }
        }

        //Convert that command-line argument from a string to an int
        int key = atoi(argv[1]);

        //Prompt user for plaintext
        string plaintext = get_string("Enter plain text: ");
        printf("ciphertext: ");

        //Iterate over each character of the plaintext:
        for (int j = 0, l=strlen(plaintext); j < l; j++) {
            if (isupper(plaintext[j])) {
                printf("%c", (((plaintext[j] - 'A') + key) % 26) + 'A');
            } else if (islower(plaintext[j])) {
                printf("%c", (((plaintext[j] - 'a') + key) % 26) + 'a');
            } else {
                printf("%c", plaintext[j] );
            }
        }
        printf("\n");
        return 0;
    } else {
        printf("Usage: ./caesar key\n");
        return 1;
    }
}

也不需要嵌套 isupper( ) islower() isalpha()中进行检查。只需使用 else if 来测试3种互斥条件:大写字母,小写字母以及其他所有内容。

There's also no need to nest the isupper() and islower() checks inside isalpha(). Just use else if to test the 3 mutually exclusive conditions: uppercase letter, lowercase letter, everything else.

并避免硬编码ASCII码,请使用字符文字。

And avoid hard-coding ASCII codes, use character literals.

这篇关于CS50凯撒密码输出错误(pset2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆