Skip to content

Instantly share code, notes, and snippets.

@teeschorle
Last active July 29, 2021 11:28
Show Gist options
  • Save teeschorle/cc998f1ef47e270282d3d4fa3d217d31 to your computer and use it in GitHub Desktop.
Save teeschorle/cc998f1ef47e270282d3d4fa3d217d31 to your computer and use it in GitHub Desktop.
CS50 Problem Set 2 (Fall 2019) - Substitution
//CS50 Problem Set 2 (Fall 2019): Substitution
//Author: teeschorle
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
bool validate(string key);
int main(int argc, string argv[])
{
if (argc == 2)
{
string key = argv[1];
if (validate(key) == true)
{
string plaintext = get_string("plaintext: ");
int charcount = strlen(plaintext);
char ciphertext[charcount];
string abc = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < charcount; i++)
{
if (isupper(plaintext[i]) != 0)
{
for (int j = 0; j < 26; j++)
{
if(abc[j] == tolower(plaintext[i]))
{
ciphertext[i] = toupper(key[j]);
break;
}
}
}
else if (islower(plaintext[i]) != 0)
{
for (int j = 0; j < 26; j++)
{
if(abc[j] == plaintext[i])
{
ciphertext[i] = tolower(key[j]);
break;
}
}
}
else
{
ciphertext[i] = plaintext[i];
}
}
printf("ciphertext: %s\n", ciphertext);
return 0;
}
else
{
printf("Please make sure your key is a permutation of all 26 characters - not more, not less.\n");
return 1;
}
}
else
{
printf("Input error. Please provide a single key.\n");
return 1;
}
}
bool validate(string key)
{
int matches = 0;
if (strlen(key) == 26)
{
for (char c = 'a'; c <= 'z'; c++)
{
for (int i = 0; i < 26; i++)
{
if(tolower(key[i]) == c)
{
matches++;
break;
}
}
}
if(matches == 26)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
@Jaga26
Copy link

Jaga26 commented Jul 29, 2021

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

//get the input from the user in comomand-line
int main (int argc, string argv[])
{
if (argc == 2) // Checks if it is two arugument including file name
{
int len = strlen(argv[1]);

    for (int i = 0; i < len; i++)        //          Checks if it is a digit, if yes, then print the following
    {
        if (isdigit(argv[1][i]))
        {
            printf("Usage: ./substitution key \n");
            exit(0);        //                          To terminate the program immediately
        }
    }
    if (len != 26)       //                          Checks if the length of the given input in command-line is 26 characters, if not, then print
    {
        printf("key must contain 26 characters.");
    }

    string plain = get_string("Plain text: ");      //Get plain text from the user to encode it

    string cipher;
    printf("Cipher text: ");

    for (int i = 0, n = strlen(plain); i < n; i++)
    {
        if (islower(plain[i]))      //              Checks the case sensitive
        {
            printf("%c", tolower(argv[1][plain[i] - 'a'])); // prints lower case cipher calue
        }
        else if (isupper(plain[i]))
        {
            printf("%c", toupper(argv[1][plain[i] - 'A'])); //prints upper case cipher value
        }
        else
        {
            printf("%c", plain[i]);             //          prints apart from alphabetical characters
        }
    }
}

else
{
    printf("Usage: ./substitution key");
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment