我的 cs50 vigenere 代码有什么问题?我接近输出

What's wrong with my cs50 vigenere code? I am close on the output

我在这上面花了好几个小时,但我仍然卡住了。我在检查时得到以下输出。这些错误与我打印出来的方式有关吗?

代码如下:

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

#define alpha_length 26

char secret(char character, int key);

int main(int argc, string argv[]) {
    //check that there are only two strings
    if (argc != 2) {
        printf("Usage: ./vignere k\n");
        return 1;
    }
    //check that argv1 is alphabetical
    string code = argv[1];
    for (int t = 0; t < strlen(code); t++) {    
        if (!isalpha(code[t])) {
            printf("Alphabetical only!\n");
            return 1;
        }
    }
    //get string from user to encrypt
    printf("plaintext: ");
    string plaintext = get_string();

    //array created out of user inputted plain text
    char cypher[strlen(plaintext)];

    //j counts the number of alphabetical characters so that it resets based on argv length
    int j = 0; 
    //iterate over characters in array.  If they are alpha then apply the function secret
    for (int i = 0; i < strlen(plaintext); i++) {
        if (isalpha(plaintext[i])) {
            int index = j % strlen(code);
            int code_index = toupper(code[index]) - 'A' ;
            cypher[i] = secret(plaintext[i], code_index);
            j = j + 1;
        } else {
            cypher[i] = plaintext[i];
        }
    }
    printf("ciphertext: %s\n", cypher);

    return 0;
}  

char secret (char character, int key) {
    char shift;

    // if the character is upper case then start with uppercase A and shift based on the appropriate character from argv1
    if (isupper(character)) {
        shift = (int)character -'A';
        shift = shift + key;
        shift = (shift % alpha_length) + 'A';
    } else {
        // else start wit lower case a
        shift = (int)character - 'a'; 
        shift = shift + key;
        shift = (shift % alpha_length) + 'a';
    }
    return (char)shift;
}

您的代码中存在多个问题:

  • 不要使用 <cs50.h> 中的 string typedef:它隐藏了你正在操作的对象的性质,一个你应该学习的简单 char * 指针毫无畏惧地掌握。

  • 因为类型 char 可以默认签名并且有一个负值,而 isalpha() 是未定义的,你应该将 char 参数转换为这些函数作为 unsigned char: isalpha((unsigned char)code[t])

  • 您打算让 cypher 成为 C 字符串,因此您必须为空终止符分配一个额外的字节并将其存储在那里:

    char cypher[strlen(plaintext) + 1];
    
  • 密码拼写为 i

这是修改后的版本:

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

#define alpha_length 26

char secret(char character, int key);

int main(int argc, char *argv[]) {
    //check that there are only two strings
    if (argc != 2) {
        printf("Usage: ./vignere k\n");
        return 1;
    }
    //check that argv1 is alphabetical
    char *code = argv[1];
    int code_len = strlen(code);
    for (int t = 0; t < code_len; t++) {    
        if (!isalpha((unsigned char)code[t])) {
            printf("Alphabetical only!\n");
            return 1;
        }
    }
    //get string from user to encrypt
    printf("plaintext: ");
    char *plaintext = get_string();
    int text_len = strlen(plaintext);

    //array created out of user inputted plain text
    char cipher[text_len + 1];

    //j counts the number of alphabetical characters so that it resets based on argv length
    int j = 0; 
    //iterate over characters in array.  If they are alpha then apply the function secret
    for (int i = 0; i < text_len; i++) {
        if (isalpha((unsigned char)plaintext[i])) {
            int index = j % code_len;
            int code_index = toupper((unsigned char)code[index]) - 'A';
            cipher[i] = secret(plaintext[i], code_index);
            j = j + 1;
        } else {
            cipher[i] = plaintext[i];
        }
    }
    cipher[text_len] = '[=11=]';
    printf("ciphertext: %s\n", cipher);

    return 0;
}  

char secret (char character, int key) {
    int shift;

    // if the character is upper case then start with uppercase A and shift based on the appropriate character from argv1
    if (isupper((unsigned char)character)) {
        shift = (int)character - 'A';
        shift = shift + key;
        return 'A' + (shift % alpha_length);
    } else {
        // else start with lower case a
        shift = (int)character - 'a'; 
        shift = shift + key;
        return 'a' + (shift % alpha_length);
    }
}