Skip to main content
added 2071 characters in body
Source Link

EditEdit2: Latest code (working)

int main (int argc, string argv[])
{
    // Checking for proper command line input
    if (argc != 2)
    {
        printf("Usage: ./substituion key\n");
        return 1;
    }

    // Checking for 26 character input key
    if (strlen(argv[1]) != 26)
    {
        printf("Key must contain 26 characters.\n");
        return 1;
    }

    // Checking for alphabetic input
    for (int i = 0; i < 26; i++)
    {
        if (argv[1][i] >= 'a' && argv[1][i] <= 'z')
        {
            argv[1][i] = argv[1][i];
        }
        else if (argv[1][i] >= 'A' && argv[1][i] <= 'Z')
        {
            argv[1][i] = argv[1][i];
        }
        else
        {
            printf("Key must only contain alphabetic characters.\n");
            return 1;
        }
    }

    // Checking for repeated characters
    for (int i = 0; i < 26; i++)
    {
        for (int j = 25; j > i; j--)
        {
            if (argv[1][i] == argv[1][j])
            {
                printf("Key must not contain repeated characters.\n");
                return 1;
            }
        }
    }

    // Removing key's sensitiity
    for (int i = 0; i < 26; i++)
    {
        if (argv[1][i] >= 'A' && argv[1][i] <= 'Z')
        {
            argv[1][i] = argv[1][i];
        }
        else
        {
            argv[1][i] -= 32;
        }
    }


    // Getting plaintext input
    string message = get_string("plaintext: ");

    // Substitution of plaintext to key
    for (int i = 0, n = strlen(message); i < n; i++)
    {
        for (int j = 0; j < 26; j++)
        {
            if (message[i] == j + 'A')
            {
                message[i] = argv[1][j];
                j = 26;
            }
            else if (message [i] == j +'a')
            {
                message[i] = argv[1][j] + 32;
                j = 26;
            }
        }
    }

    // Ciphertext
    printf("ciphertext: %s\n", message);
    return 0;
}

[OUTDATED/ANSWERED] Edit: Here's my code.

Edit: Here's my code.

Edit2: Latest code (working)

int main (int argc, string argv[])
{
    // Checking for proper command line input
    if (argc != 2)
    {
        printf("Usage: ./substituion key\n");
        return 1;
    }

    // Checking for 26 character input key
    if (strlen(argv[1]) != 26)
    {
        printf("Key must contain 26 characters.\n");
        return 1;
    }

    // Checking for alphabetic input
    for (int i = 0; i < 26; i++)
    {
        if (argv[1][i] >= 'a' && argv[1][i] <= 'z')
        {
            argv[1][i] = argv[1][i];
        }
        else if (argv[1][i] >= 'A' && argv[1][i] <= 'Z')
        {
            argv[1][i] = argv[1][i];
        }
        else
        {
            printf("Key must only contain alphabetic characters.\n");
            return 1;
        }
    }

    // Checking for repeated characters
    for (int i = 0; i < 26; i++)
    {
        for (int j = 25; j > i; j--)
        {
            if (argv[1][i] == argv[1][j])
            {
                printf("Key must not contain repeated characters.\n");
                return 1;
            }
        }
    }

    // Removing key's sensitiity
    for (int i = 0; i < 26; i++)
    {
        if (argv[1][i] >= 'A' && argv[1][i] <= 'Z')
        {
            argv[1][i] = argv[1][i];
        }
        else
        {
            argv[1][i] -= 32;
        }
    }


    // Getting plaintext input
    string message = get_string("plaintext: ");

    // Substitution of plaintext to key
    for (int i = 0, n = strlen(message); i < n; i++)
    {
        for (int j = 0; j < 26; j++)
        {
            if (message[i] == j + 'A')
            {
                message[i] = argv[1][j];
                j = 26;
            }
            else if (message [i] == j +'a')
            {
                message[i] = argv[1][j] + 32;
                j = 26;
            }
        }
    }

    // Ciphertext
    printf("ciphertext: %s\n", message);
    return 0;
}

[OUTDATED/ANSWERED] Edit: Here's my code.

added 2345 characters in body
Source Link

Edit: Here's my code.

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

int main (int command, string line[])
{
    // Checking for proper command line input
    if (command == 2)
    {
        // Checking for 26 character input key
        if (strlen(line[1]) == 26)
        {
            // Checking for alphabetic input
            for (int i = 0; i < 26; i++)
            {
                if (line[1][i] >= 'a' && line[1][i] <= 'z')
                {
                    line[1][i] = line[1][i];
                }
                else if (line[1][i] >= 'A' && line[1][i] <= 'Z')
                {
                    line[1][i] = line[1][i];
                }
                else
                {
                    printf("Key must only contain alphabetic characters.\n");
                    return 1;
                }
            }

            // Checking for repeated characters
            for (int i = 0; i < 26; i++)
            {
                for (int j = 25; j > i; j--)
                {
                    if (line[1][i] == line[1][j])
                    {
                        printf("Key must not contain repeated characters.\n");
                        return 1;
                    }
                }
            }

            // Getting plaintext input
            string message = get_string("plaintext: ");

            // Substitution of plaintext to key
            for (int i = 0, n = strlen(message); i < n; i++)
            {
                for (int j = 0; j < 26; j++)
                {
                    if (message[i] == j + 'A')
                    {
                        message[i] = line[1][j];
                        j = 26;
                    }
                    else if (message [i] == j +'a')
                    {
                        message[i] = line[1][j] + 32;
                        j = 26;
                    }
                }
            }

            printf("ciphertext: %s\n", message);
            return 0;
        }
        else
        {
            printf("Key must contain 26 characters.\n");
            return 1;
        }
    }
    else
    {
        printf("Usage: ./substituion key\n");
        return 1;
    }
}

enter image description here

enter image description here

Edit: Here's my code.

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

int main (int command, string line[])
{
    // Checking for proper command line input
    if (command == 2)
    {
        // Checking for 26 character input key
        if (strlen(line[1]) == 26)
        {
            // Checking for alphabetic input
            for (int i = 0; i < 26; i++)
            {
                if (line[1][i] >= 'a' && line[1][i] <= 'z')
                {
                    line[1][i] = line[1][i];
                }
                else if (line[1][i] >= 'A' && line[1][i] <= 'Z')
                {
                    line[1][i] = line[1][i];
                }
                else
                {
                    printf("Key must only contain alphabetic characters.\n");
                    return 1;
                }
            }

            // Checking for repeated characters
            for (int i = 0; i < 26; i++)
            {
                for (int j = 25; j > i; j--)
                {
                    if (line[1][i] == line[1][j])
                    {
                        printf("Key must not contain repeated characters.\n");
                        return 1;
                    }
                }
            }

            // Getting plaintext input
            string message = get_string("plaintext: ");

            // Substitution of plaintext to key
            for (int i = 0, n = strlen(message); i < n; i++)
            {
                for (int j = 0; j < 26; j++)
                {
                    if (message[i] == j + 'A')
                    {
                        message[i] = line[1][j];
                        j = 26;
                    }
                    else if (message [i] == j +'a')
                    {
                        message[i] = line[1][j] + 32;
                        j = 26;
                    }
                }
            }

            printf("ciphertext: %s\n", message);
            return 0;
        }
        else
        {
            printf("Key must contain 26 characters.\n");
            return 1;
        }
    }
    else
    {
        printf("Usage: ./substituion key\n");
        return 1;
    }
}

enter image description here

Source Link

Can't figure out why check50 sad faced these two

enter image description here

Hi, I found a similar question asked here but I couldn't quiet get the answer provided. The answer said that the outputs must be the same, but the outputs are the same.

The three similar keys (YUKFRN...) used to test by check50 in substitution.c seems to be for checking the case sensitivity of the code. The ciphertext for these three similar keys must be the same then, and my code did output same ciphertexts for each of them. But why was it wrong somehow? Thanks in advance.