0

I'm currently struggling with a bug in caesar pset2. If i use check 50 everything is green except:

:( encrypts "a" as "b" using 1 as key
    output not valid ASCII text

:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
    output not valid ASCII text

My code:

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



int main(int argc, string argv[])

{

    // Get key
    if (argc != 2)
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
    
    char *k = argv[1];
    int size = strlen(k);
    
    for (int i = 0; i < size; i++)
    {
        if (isdigit(k[i]) == 0)
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    }
    
    int kn = atoi(argv[1]);
    
    // Get plaintext -> Get-string 
    string input = get_string("plaintext: ");
    int isize = strlen(input);
    
    char cryp[isize];
    int j = 0;
    for (j = 0; j < isize; j++)
    {
        if ((isalpha(input[j]) > 0) && (isupper(input[j])))
        {
            cryp[j] = (((input[j] - 65 + (kn % 26)) % 26) + 65);
        }
        if ((isalpha(input[j]) > 0) && (islower(input[j])))
        {
            cryp[j] = (((input[j] - 97 + (kn % 26)) % 26) + 97);
        }
        if (isalpha(input[j]) == 0)
        {
            cryp[j] = input[j];
        }
    }
    printf("ciphertext: %s\n", cryp);
    return 0;
}

1 Answer 1

0

Interesting. Where is the end of string marker in cryp?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .