Here is my code.
Apologies if it is hard to understand / messy.
When I check50 it shows this.
I have a brief idea on how to solve the invalid characters in key but I don't mind some help on that too.
My main question is for all the encryption as I am getting the correct answer.
:) substitution.c exists
:) substitution.c compiles
:( encrypts "A" as "Z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
expected "ciphertext: Z\...", not "ciphertext: Z\..."
:( encrypts "a" as "z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
expected "ciphertext: z\...", not "ciphertext: z\..."
:( encrypts "ABC" as "NJQ" using NJQSUYBRXMOPFTHZVAWCGILKED as key
expected "ciphertext: NJ...", not "ciphertext: NJ..."
:( encrypts "XyZ" as "KeD" using NJQSUYBRXMOPFTHZVAWCGILKED as key
expected "ciphertext: Ke...", not "ciphertext: Ke..."
:( encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZTEOGXHCIPJSQD as key
expected "ciphertext: Cb...", not "ciphertext: Cb..."
:( encrypts "This is CS50" as "Cbah ah KH50" using yukfrnlbavmwzteogxhcipjsqd as key
expected "ciphertext: Cb...", not "ciphertext: Cb..."
:( encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZteogxhcipjsqd as key
expected "ciphertext: Cb...", not "ciphertext: Cb..."
:( encrypts all alphabetic characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key
expected "ciphertext: Rq...", not "ciphertext: Rq..."
:) handles lack of key
:) handles invalid key length
:( handles invalid characters in key
timed out while waiting for program to exit
:) handles duplicate characters in key
:) handles multiple duplicate characters in key
my code
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main (int argc , string argv[])
{
int i, u, y, g, z, r, G, check, check2;
//when no key is added
if(argc == 1)
{
printf("Usage: ./substitution key\n");
return 1;
}
//When a digit is entered
z = atoi(argv[1]);
if(z != 0)
{
printf("Usage: ./substitution key\n");
return 1;
}
//When more than 1 key is added
if(argc != 2)
{
printf("Usage: ./substitution key\n");
return 1;
}
//When key has duplicate letters
for(check = 0; check <= strlen(argv[1]); check++)
{
for(check2 = 1 + check; check2 <= strlen(argv[1]); check2++)
{
if(argv[1][check] == argv[1][check2])
{
printf("Usage: ./substitution key\n");
return 1;
}
}
}
//When key does not have 26 characters
if(argc == 2 && strlen(argv[1]) != 26)
{
printf("Key must contain 26 characters.\n");
return 1;
}
//Correct input
//converting to string
if(argc == 2 && strlen(argv[1]) == 26)
{
string user_input = get_string("plaintext: ");
printf("ciphertext: ");
//g is the char corresponding to i
for(u = 0; u <= strlen(user_input); u++)
{
if(user_input[u] >= 'A' && user_input[u] <= 'Z')
{
for(y = 'A'; y <= user_input[u]; y++)
{
g = (toupper(argv[1][y - 'A']));
{
G = g - (y);
}
}
}
else if(user_input[u] >= 'a' && user_input[u] <= 'z')
{
for(r = 'a'; r <= user_input[u]; r++)
{
g = (toupper(argv[1][r - 'a']));
{
G = g - (r - 32);
}
}
}
else
{
G = 0;
}
//i is corresponding digit in code
printf("%c", user_input[u] + G);
}
printf("\n");
}
}