0

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");

    }


}

0

When dealing with strings or char arrays, you have to remember that 'strlen(mystring)' returns the length of the variable, but as the first index is 0, the expression 'mystring[length]' is reaching out of the limit of the string. So, be careful when using 'strlen' in a 'for' loop.

About invalid characters, I would recommend you to check it with your own code, as it'll improve your skills with text managment. An alternative solution, if you don't feel like doing it yourself, would be the use of 'isalpha'.

1
  • Ohh I understand now. I changed the <= to < and it works! thank you very much. as for the invalid characters, I have figured it out already thank you! May I ask however how does reaching out of the limit of the string affect the solution? If I reach out of the limit of the string by 1 wouldn't it be '\0' which would not affect the output? – anahop May 26 '20 at 12:41

You must log in to answer this question.

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