Skip to content

Instantly share code, notes, and snippets.

@Latishfaction
Last active January 21, 2024 04:46
Show Gist options
  • Save Latishfaction/b76758b11105e25342c0aac201d6d7e6 to your computer and use it in GitHub Desktop.
Save Latishfaction/b76758b11105e25342c0aac201d6d7e6 to your computer and use it in GitHub Desktop.
CS50 (cs50x) pset2 Substitution Solution 2020
#include <stdio.h>
#include <cs50.h> // to get plain text
#include <string.h> // for strlen()
#include <ctype.h> // for string operation upper, lower
void Do_substitute(); // for further substitute
void alpha_arr_val(char pos, string key);
// to get the alphabet array value of each plain text element (ex:plaintext = h|alphabet array = 8 {a=0, b=1 .....z=26})
int main(int argc, string argv[])
{
if (argc == 2) // number of commands in terminal
{
if (strlen(argv[1]) == 26)
{
for (int i = 0; i < strlen(argv[1]) ; i++) // checking each element of string
{
if (! isalpha(argv[1][i])) // if contain non-alphabet
{
printf("Key must contain 26 characters.\n");
return 1;
}
for (int j = i + 1 ; j < strlen(argv[1]) ; j++) // checking to the next element of arg[i]
{
if (toupper(argv[1][j]) == toupper(argv[1][i])) // checking repeated element
{
printf("Key must not contain repeated alphabets.\n");
return 1;
}
}
}
Do_substitute(argv[1]);
}
else
{
printf("Key must contain 26 characters.\n");
return 1;
}
}
else
{
printf("Usage: ./substitution key\n");
return 1;
}
return 0;
}
void Do_substitute(string key)
{
string p = get_string("plaintext: ");
printf("ciphertext: ");
for (int i = 0; i < strlen(p); i++)
{
if (isalpha(p[i])) // checking p[i] is alphabet
{
char x = p[i];
if (islower(p[i]))
{
alpha_arr_val(tolower(x), key); // passing p[i] in lower-case
}
else
{
alpha_arr_val(toupper(x), key); // passing p[i] in UPPER-CASE
}
}
else
{
printf("%c", p[i]); // print the element as it is (such as space | , | special characters | ? | etc...)
}
}
printf("\n");
}
void alpha_arr_val(char pos, string key) // passing p[i] and upper/lower(alpha array)
{
string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // alphabet array
for (int i = 0; i < strlen(alpha); i++) // accessing each element in alpha array
{
if (islower(pos)) // p[i] is lower
{
if (pos == tolower(alpha[i]))
{
printf("%c", tolower(key[i]));
}
}
else // for UPPERCASE // p[i] is upper
{
if (pos == toupper(alpha[i]))
{
printf("%c", toupper(key[i]));
}
}
}
}
@Aquaghost27
Copy link

Aquaghost27 commented Nov 13, 2023 via email

@mohawkart
Copy link

mohawkart commented Jan 21, 2024

Why does it return 1?

Return 1 is another thing for saying that if the input doesn't meet the requirement, it will assume it is false and stop the function and go the next function rather than repeating it, causing errors, and stopping it from going to the next function. Similarly, return 0 is the opposite, it means it will return true, stop the function, and move to the next function.

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