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;
}
}
Copy link

ghost commented Feb 6, 2021

This function returns a non-zero int if c is an uppercase letter and 0 if c is not an uppercase letter.

@M-Ali-Horya
Copy link

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

int main(int argc, string argv[])
{
if (argc != 2 || !isalpha(*argv[1]))
{
printf("Usage: ./sub key\n");
return (1);
}
string key = argv[1];
if (strlen(key) != 26)
{
printf("Usage: ./sub key\n");
return (1);
}
string s = get_string("Plaintext: ");
printf("Ciphertext: ");
for (int i = 0, n = strlen(s); i < n; i++)
{
if (isupper(s[i]))
{
char u = s[i];
char m = 'A';
if (islower(s[i]))
m = 'a';
printf("%c",key[u - m] );
}
else
{
printf("%c",s[i]);
}
}
printf("\n");
return(0);
}

@nickiliopoulosedu
Copy link

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

bool validate_key(string key);

int main(int argc, string argv[])
{
    //checks if key is the right length
    if (argc != 2 || strlen(argv[1]) != 26 || validate_key((string) argv[1]) != 1)
    {
        printf("Key must exist and contain 26 non identical alphabetical characters.\n");
        return 1;
    }

    //gets input text

    string text = get_string("plaintext: ");

    int len = strlen(text);
    char mychar = toupper(text[0]) - 65;
    for (int i = 0; i < len; i++)
    {
        if (mychar >= 0 && mychar <= 26)
        {
            if (isupper(text[i]))
            {
                text[i] = toupper((char) argv[1][(int) mychar]);
            }
            else
            {
                text[i] = tolower((char) argv[1][(int) mychar]);
            }
        }
        mychar = toupper(text[i + 1]) - 65;
    }

    printf("ciphertext: %s\n", text);
    return 0;
}

bool validate_key (string key)
{
    int len = strlen(key);
    char mychar;
    for(int i = 0; i < len; i++)
    {
        mychar = toupper(key[i]);
        if(mychar >= 65 && mychar <= 90)
        {
            for(int j = i + 1; j < len; j++)
            {
                if(mychar == toupper(key[j]))
                {
                    return 0;
                }
            }
        }
        else
        {
            return 0;
        }
    }
    return 1;
}

@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