Last active
January 21, 2024 04:46
-
-
Save Latishfaction/b76758b11105e25342c0aac201d6d7e6 to your computer and use it in GitHub Desktop.
CS50 (cs50x) pset2 Substitution Solution 2020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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
commented
Nov 13, 2023
via email
this was posted like a billion years ago
…On Mon, Nov 13, 2023 at 12:57 PM khoaoaoaoa ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
for me, it is kinda simple
`#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int check_CLIargs(int argc, string argv[]);
string encrypt_plaintext(string plaintext, string key);
int main(int argc, string argv[])
{
int status = check_CLIargs(argc, argv);
if (status == 1)
{
return 1;
}
string plaintext = get_string("plaintext: ");
string ciphertext = encrypt_plaintext(plaintext, argv[1]);
printf("ciphertext: %s\n", ciphertext);
return 0;
}
int check_CLIargs(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage: ./substitution key\n");
return 1;
}
else if (strlen(argv[1]) != 26)
{
printf("Key must contain 26 characters.\n");
return 1;
}
for (int i = 0; i < 26; i++)
{
if (!isalpha(argv[1][i]))
{
printf("Key must contain 26 alphabetical letters.\n");
return 1;
}
for (int j = i + 1; j < 26; j++)
{
if (tolower(argv[1][j]) == tolower(argv[1][i]))
{
printf("Key must contain 26 distinct letters.\n");
return 1;
}
}
}
return 0;
}
string encrypt_plaintext(string plaintext, string key)
{
string ciphertext = plaintext;
for (int i = 0, n = strlen(plaintext); i < n; i++)
{
if (islower(plaintext[i]))
{
ciphertext[i] = tolower(key[plaintext[i] - 97]);
}
else if (isupper(plaintext[i]))
{
ciphertext[i] = toupper(key[plaintext[i] - 65]);
}
}
return ciphertext;
}
`
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/Latishfaction/b76758b11105e25342c0aac201d6d7e6#gistcomment-4759431>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHPHDEVJH6QR2OSF53254HTYEJNRHBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTAMZUG42DMMRUU52HE2LHM5SXFJTDOJSWC5DF>
.
You are receiving this email because you commented on the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
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