Last active
July 17, 2025 11:20
-
-
Save Juskr04/ac6e72c25532cf9edf0f625bec852f07 to your computer and use it in GitHub Desktop.
cs50 pset2 substitution problem
This file contains hidden or 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> | |
| int calculate_key_size(char argv[]); // returns size of key | |
| int is_alphabet_arr(char argv[]); // checks whether all elements of an array are alphabets | |
| int is_alphabet_char(char a); // checks whether a character is an alphabet | |
| int repeat_letter_check(char argv[]); // checks whether a letter repeats in an array | |
| int check_key_validity(char argv[]); // checks whether the key given by the user is valid or not according to constraints | |
| int case_check(char a); // returns 0 for lowercase and 1 for uppercase | |
| char uppercase_alphabets[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; | |
| char lowercase_aplhabets[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; | |
| int main(int argc, char* argv[]){ | |
| if(argc != 2){ | |
| printf("Usage : ./program_name key"); | |
| return 1; | |
| } | |
| int key_validity = check_key_validity(argv[1]); | |
| if(key_validity != 0){ | |
| printf("INVALID KEY\n"); | |
| return 1; | |
| } | |
| char plaintext[1000]; | |
| char ciphertext[1000]; | |
| printf("plaintext: "); | |
| fgets(plaintext, sizeof(plaintext), stdin); | |
| //encrypting with user key | |
| int i = 0; | |
| while(plaintext[i] != '\0'){ | |
| for(int y = 0; y < 26; y++){ | |
| if(is_alphabet_char(plaintext[i]) == 1){ | |
| ciphertext[i] = plaintext[i]; | |
| break; | |
| } | |
| else if(plaintext[i] == lowercase_aplhabets[y]){ | |
| ciphertext[i] = argv[1][y]; | |
| break; | |
| } | |
| else if(plaintext[i] == uppercase_alphabets[y]){ | |
| ciphertext[i] = argv[1][y]; | |
| break; | |
| } | |
| } | |
| //preserving case | |
| int plaintext_casecheck = case_check(plaintext[i]); | |
| int ciphertext_casecheck = case_check(ciphertext[i]); | |
| if (plaintext_casecheck != ciphertext_casecheck){ | |
| if(plaintext_casecheck == 0){ | |
| ciphertext[i] = (char)((int)(ciphertext[i] + 32)); | |
| } | |
| else if(plaintext_casecheck == 1){ | |
| ciphertext[i] = (char)((int)(ciphertext[i] - 32)); | |
| } | |
| } | |
| i++; | |
| } | |
| printf("ciphertext: %s", ciphertext); | |
| return 0; | |
| } | |
| int calculate_key_size(char argv[]){ | |
| int i = 0; | |
| int key_size = 0; | |
| while(argv[i] != '\0'){ | |
| key_size += 1; | |
| i++; | |
| } | |
| if(key_size != 26) | |
| return 1; | |
| return 0; | |
| } | |
| int is_alphabet_arr(char argv[]){ | |
| int a = 0; | |
| while(argv[a] != '\0'){ | |
| if(!(((int)argv[a] >= 65 && (int)argv[a] <= 90) || ((int)argv[a] >= 97 && (int)argv[a] <= 122))){ | |
| return 1; | |
| } | |
| a++; | |
| } | |
| return 0; | |
| } | |
| int is_alphabet_char(char a){ | |
| for(int j = 65, k = 97; j < 91; j++, k++){ | |
| if((int)a == j || (int)a == k) | |
| return 0; | |
| } | |
| return 1; | |
| } | |
| int repeat_letter_check(char argv[]){ | |
| int c = 0; | |
| char alphabets[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; | |
| // above array's content does not matter; | |
| for(int b = 0; b < 27; b++){ | |
| for(int j = 65, k = 97; j < 91; j++, k++){ | |
| if((int)argv[b] == j || (int)argv[b] == k){ | |
| alphabets[j-65] = '!'; | |
| break; | |
| } | |
| } | |
| } | |
| while(argv[c] != '\0'){ | |
| if(alphabets[c] != '!'){ | |
| return 1; | |
| } | |
| c++; | |
| } | |
| return 0; | |
| } | |
| int check_key_validity(char argv[]){ | |
| if(calculate_key_size(argv) == 1) | |
| return 1; | |
| if(is_alphabet_arr(argv) == 1) | |
| return 1; | |
| if(repeat_letter_check(argv) == 1) | |
| return 1; | |
| return 0; | |
| } | |
| int case_check(char a){ | |
| for(int i = 0; i < 26; i++){ | |
| if(a == lowercase_aplhabets[i]){ | |
| return 0; | |
| } | |
| else if(a == uppercase_alphabets[i]){ | |
| return 1; | |
| } | |
| } | |
| return -1; //can return any value as such because execution will never reach here | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment