Skip to content

Instantly share code, notes, and snippets.

@ThatOneCubanDude
Created March 26, 2020 22:57
Show Gist options
  • Save ThatOneCubanDude/016a4f3e2855b3958f5e8822b5a78786 to your computer and use it in GitHub Desktop.
Save ThatOneCubanDude/016a4f3e2855b3958f5e8822b5a78786 to your computer and use it in GitHub Desktop.
My solution to CS50x Problem Set 2 - Substitution. Feel free to critique or ask questions.
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
//checks if user has inputed a command line argument, if not returns error message
if (argc == 2)
{
//checks if command line argument is 25 characters long, if not returns error message
if (strlen(argv[1]) == 26)
{
int count = 0;
for (int i = 0, n = strlen(argv[1]); i < n; i++)
{
//checks if each character in command line argument is a letter, if not returns error message
if (isalpha(argv[1][i]) == 0)
{
printf("Usage: key must only contain alphabetical characters\n");
return 1;
}
for (int p = 0; argv[1][p] != '\0'; p++)
{
//counts each character and each repitition of character and stores it in variable "count"
if (argv[1][p] == argv[1][i])
{
count++;
}
}
}
//if count is larger than 26 due to a repitition, returns error message
if (count != 26)
{
printf("Usage: key must not contain repeating characters\n");
return 1;
}
}
else
{
printf("Usage: key must be 26 characters long\n");
return 1;
}
}
else
{
printf("Usage: ./substitution key\n");
return 1;
}
//gets the text the user wants to encode and stores it in variable "plaintext"
string plaintext = get_string("plaintext: ");
//stores command line argument (user's intended key) in variable "key" for neater code later on
string key = argv[1];
for (int i = 0, n = strlen(plaintext); i < n; i++)
{
//makes sure only letters are encoded
if (isalpha(plaintext[i]) != 0)
{
//separate algorithm for uppercase letters in the user's text-to-be-encoded
if (isupper(plaintext[i]))
{
//establishes variable "key_place" for matching the appropriate letter to the key's equivalent
int key_place = (plaintext[i] - 65);
//if the letter in the key that is supposed to replace the original letter is of different case, switches the case in key
if (islower(key[key_place]) != 0)
{
key[key_place] -= 32;
}
//substitutes the original letter with the corresponding letter in key (which has now been case-matched)
plaintext[i] = key[key_place];
}
//separate algorithm for lowercase letters in the user's text-to-be-encoded
else if (islower(plaintext[i]) != 0)
{
//establishes variable "key_place" for matching the appropriate letter to the key's equivalent
int key_place = (plaintext[i] - 97);
//if the letter in the key that is supposed to replace the original letter is of different case, switches the case in key
if (isupper(key[key_place]) != 0)
{
key[key_place] += 32;
}
//substitutes the original letter with the corresponding letter in key (which has now been case-matched)
plaintext[i] = key[key_place];
}
}
}
//prints out the encoded text and ends program
printf("ciphertext: %s\n", plaintext);
return 0;
}
@tjhartline
Copy link

Thank you for posting this!! I have a hard time getting started or should I say knowing where to start! You gave clear and precise comments stating the purpose, so now I can go through the order of steps creating mine!

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