0
The Errors- 
    :) 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
  

The Code-

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


int main(int argc, char* key[])

{

// Vaildate key



int no[26] = {0};

string k = key[1];

if (argc != 2)

{

printf("Enter a key.\n");

return 1;

}

int len = strlen(key[1]);

if (len != 26)

{

printf("Key must contain 26 characters.\n");

return 1;

}

for (int x = 0; x < len; x++)

{

if (islower(k[x]) || isupper(k[x]))

{

no[tolower(k[x])-97] ++;

if (no[tolower(k[x])-97] > 1)

{

printf("Key must have no repeated characters.\n");

return 1;

}

}

else

{

printf("Key needs to contain only letters.");

return 1;

}

}



// Get input



string i = get_string("plaintext: ");



// Encypt input

char output[strlen(i)];

for (int x = 0; x < strlen(i); x++)

if (islower(i[x]))

{

output[x] = tolower(k[i[x] - 97]);

}

else if (isupper(i[x]))

{

output[x] = toupper(k[i[x] - 65]);

}

else

{

output[x] = i[x];

}

// Print output

printf ("ciphertext: %s\n", output);

return 0;
}
0

Is ouptut allocated correctly? It is declared here char output[strlen(i)]; as a char array, and used here printf ("ciphertext: %s\n", output); as a string. It is missing the "thing that makes a string a string", i.e. the null terminator '\0'.

output 1) isn't allocated to include the null-terminator and 2) isn't null terminated.

| improve this answer | |

You must log in to answer this question.

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