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]));
}
}
}
}
@Tomi-3-0
Copy link

Tomi-3-0 commented Jan 9, 2023

#line 6 you might consider removing the check_key () function on line 6, seems redundant.
#line 61 you couldmerge the printf("ciphetext: ") statement on line 61 and merged it with that on line 81.

@Latishfaction
Copy link
Author

#line 6 you might consider removing the check_key () function on line 6, seems redundant. #line 61 you couldmerge the printf("ciphetext: ") statement on line 61 and merged it with that on line 81.

Dear @Tomi-3-0 ,
Thankyou for your suggestion on #line 6 , the checkey() function was declared but never initialized, I've updated the changes.

#line 61, if we try to merge the printf's on line 61 and line 81, since it is written inside the loop we see "ciphertext : " repeatedly which may be not look pleasing as per viewing experience. But I encourage you to try to merge the lines as per your convenience. :)

@Latishfaction
Copy link
Author

Why does it return 1?

return 1 means interrupting the execution of a code and aborting the program. Please notice line no 21,30, 41,48 these printf statements are indicating error, and to terminate the program so that it will not move further we use return 1 (because the main function is initialized with int return type).
return 0 for successfully exiting from the program.

@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