Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- //elements used for the encrypt function
- string abcArray = "abcdefghijklmnopqrstuvwxyz";
- string ABCArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- int encrypt(string text, string argv[]); //encrypt function
- int main(int argc, string argv[])
- {
- if (argc==2) //check amount of command line arguments
- {
- if (strlen(argv[1])==26) // check amount of characters in the key
- {
- for(int i=0; i<26; i++)
- {
- if((argv[1][i]>=65 && argv[1][i]<=97) || (argv[1][i]>=97 && argv[1][i]<=122)) //check type of character in key
- {
- for(int p=i+1; p<26; p++) //check repeating letters in key
- {
- if ((argv[1][i] != argv[1][p]) && ((argv[1][i]+32) != argv[1][p]) && ((argv[1][i]-32) != argv[1][p]))
- {
- }
- else
- {
- printf("The key must not contain the same letter twice\n");
- return 1;
- }
- }
- }
- else
- {
- printf("Key must only contain alphabetic characters\n");
- return 1;
- }
- }
- }
- else
- {
- printf("Key must contain 26 characters\n");
- return 1;
- }
- }
- else
- {
- printf("Usage: ./substitution key\n");
- return 1;
- }
- string t = get_string("plaintext: ");
- printf("ciphertext: %c", encrypt(t, argv));
- printf("\n");
- return 0;
- }
- //encrypt function
- int encrypt(string text, string argv[])
- {
- for(int x = 0; x <= strlen(text); x++)
- {
- if ((text[x]<65) || (text[x]>90 && text[x]<97) || (text[x]>122))
- {
- printf("%c", text[x]);
- }
- else if ((text[x]>=65) && (text[x]<=90))
- {
- for(int y=0; y<=strlen(ABCArray); y++)
- {
- if(text[x]==ABCArray[y])
- {
- printf("%c", toupper(argv[1][y]));
- }
- }
- }
- else
- {
- for(int z=0; z<=strlen(abcArray); z++)
- {
- if(text[x]==abcArray[z])
- {
- printf("%c", tolower(argv[1][z]));
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement