Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <cs50.h>
- #include <string.h>
- int main(int argc, string argv[])
- {
- (void)argc;
- if (argv[1] != NULL)
- {
- string k = (argv[1]);
- string p = get_string("plaintext: \n");
- string c = p; //declaring "c" as a variable for the cipher
- printf("ciphertext: ");
- int j = 0;
- int m = strlen(k);
- for (int i = 0, n = strlen(p); i < n; i++)
- {
- if isalpha(p[i]) //checks if letter
- {
- if isupper(p[i]) //preserve case
- {
- if isupper(k[m]) //checks case of k[m]
- {
- c[i] = (char)((p[i] + k[m] - 130) % 26 + 65); //askii to alpha adjust to askii
- printf("%c", p[i]); //add character to string cipher
- }
- else //checks case of k[m]
- {
- c[i] = (char)((p[i] + k[m] - 162) % 26 + 65);
- printf("%c", p[i]);
- }
- }
- else //preserve case
- {
- if isupper(k[m])
- {
- c[i] = (char)((p[i] + k[m] - 162) % 26 + 97); //askii to alpha adjust to askii
- printf("%c", p[i]); //add character to string cipher
- }
- else
- {
- c[i] = (char)((p[i] + k[m] - 194) % 26 + 97);
- printf("%c", p[i]);
- }
- }
- j++; //counts to next character in key
- }
- else //prints non-letters
- {
- printf("%c", p[i]);
- }
- if (m == j) //checks if all characters in k have been used
- {
- j = 0; //reset j
- }
- }
- printf("\n");
- return 0;
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement