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>
- #include <unistd.h>
- int main(int argc, char* key[])
- {
- string k = key[1];
- // checking if totoal number of arcument in 2
- if (argc != 2 )
- {
- printf("Usage: ./substitution key");
- return 1;
- }
- // checking if all the given arguments are letters loop is to check every element
- for (int i = 0; i <26; i++)
- {
- if (!isalpha(*(k+i)))
- {
- printf("Usage: ./substitution key");
- return 1;
- }
- }
- int len = strlen(k);
- //checking if given argument has length of 26
- if (len != 26)
- {
- printf("Key must contain 26 characters.\n");
- return 1;
- }
- // checking ig there are any duplicate keys
- for (int i=0; i<25; i++)
- {
- for (int j=i+1; j<26; j++)
- {
- if (k[i] == k[j])
- {
- printf("Key must not contain double character.");
- exit (1);
- }
- }
- }
- //getting user input
- string plaintext = get_string("plaintext: ");
- printf("ciphertext: ");
- //converting plain text into cypher text
- for (int i = 0, j = 0, n = strlen(plaintext); i < n; i++)
- {
- if (islower(plaintext[i]))
- {
- printf("%c", tolower(k[plaintext[i] - 97]));
- j++;
- }
- else if (isupper(plaintext[i]))
- {
- printf("%c", toupper(k[plaintext[i] - 65]));
- j++;
- }
- else
- {
- printf("%c", plaintext[i]);
- j++;
- }
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement