加密不输出 ASCII 字符串

Encryption does not output an ASCII string

在 CS50 中,我正试图结束我的替换练习,我遇到了一个问题,但不知道如何解决。

这是我的代码:

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

int get_validkey(string Text);
int get_Alpha_to_code(char charac);


int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("usage: ./substitution key\n");

        return 1;
    }

    int validation = get_validkey(argv[1]);
    if (validation != 0)
    {
        if(validation == 1)
        {
            printf("key must contain 26 alphabetical characters\n");
        }
        else
        {
            if (validation == 2)
            {
                printf("some charaters are not alphabetic\n");
            }
            else
            {
                printf("some charaters are repeated\n");
            }
        }
        return 1;
    }
    else
    {
        // constants

        string code = argv[1];
        int charc;
        int j;
        // ask for message to encrypt
        string tocode = get_string("plaintext: ");
        // transform to code
        int charcount = strlen(tocode);
        char codedmessage[charcount];
        for (int i = 0; i <  strlen(tocode); i++)
        {
            // check type of character
            if ((tocode[i]>='a' && tocode[i]<='z') || (tocode[i]>='A' && tocode[i]<='Z'))
            {
                j = get_Alpha_to_code(tocode[i]);
                if(islower(tocode[i]))
                {
                    codedmessage[i] = tolower(code[j]);
                }
                else
                {
                    codedmessage[i] = toupper(code[j]);
                }
            }
            else
            {
                codedmessage[i] = tocode[i];
            }  
        }
        codedmessage[strlen(codedmessage)] = '[=10=]';
        printf("ciphertext: %s", codedmessage);
        printf("\n");
        return 0;    
    }   
}
// function assesses if the key input is valid and returns 0 if it is and 1 if it is not
int get_validkey(string Text)
{
    int inputlength = strlen(Text);
    if (inputlength != 26)
    {
        return 1;
    }
    else
    {   
        for (int g = 0; g < 26; g++)
        {
            // checks if the character is non alphabetical
            char chartest = toupper(Text[g]);

            if (chartest < 'A' || chartest > 'Z')
            {
                return 2;
            }
            // scans all characters before A[g] to see if it has already been used
            for (int k = 0; k < g; k++)
            {
                char beforechar = toupper(Text[k]);

                if (chartest == beforechar)
                {
                    return 3;
                }   
            }
        }
        return 0;
    }
}

int get_Alpha_to_code(char charac)
{
    // define order for alphabet
    const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char Alphachar = '[=10=]';
    // look at char position in alphabet
    char chartest = toupper(charac);
    // find position of charac in chain
    int k = 0;
    while (chartest != Alphachar)
    {
        Alphachar = Alphabet[k];
        k++;
    }
    // send back char in code
    return k - 1;   
}

检查结果为:

:) substitution.c exists :) substitution.c compiles :( encrypts "A" as "Z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key output not valid ASCII text 
:( encrypts "a" as "z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
    output not valid ASCII text 
:( encrypts "ABC" as "NJQ" using NJQSUYBRXMOPFTHZVAWCGILKED as key
    output not valid ASCII text 
:( encrypts "XyZ" as "KeD" using NJQSUYBRXMOPFTHZVAWCGILKED as key
    output not valid ASCII text 
:) encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZTEOGXHCIPJSQD as key 
:) encrypts "This is CS50" as "Cbah ah KH50" using yukfrnlbavmwzteogxhcipjsqd as key 
:) encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZteogxhcipjsqd as key 
:) encrypts all alphabetic characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key 
:) handles lack of key :) handles invalid key length 
:) handles invalid characters in key 
:) handles duplicate characters in key 
:) handles multiple duplicate characters in key

我的结果似乎有效,因为 'A' 我有 'Z','a' 我有 'z',...

但是检查系统无法将我的输出识别为 ASCII。

你的问题在那一行:

codedmessage[strlen(codedmessage)] = '[=10=]';

可以

codedmessage[i] = '[=11=]';

codedmessage[charcount] = '[=12=]';

或更贵白白

codedmessage[strlen(tocode)] = '[=13=]';

因为你不能在 codedmessage 上使用 strlen 来放置空字符结束它(这就是你想要做的),所以你(可能是) 准确地在你(可能)找到它的位置重写 0,它可以在 codedmessage 之外并且行为未定义。在下一行 printf 中写入字符,直到它(可能)找到一个空字符,因此从中写入非初始化字符,行为再次未定义

除此之外,在:

   int charcount = strlen(tocode);
   char codedmessage[charcount];
   for (int i = 0; i <  strlen(tocode); i++)

你知道长度是 charcount 为什么你每次调用 strlen(tocode) 知道它是不变的,它的值是多少?