Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- int shift(char q);// declaring function to handle keyword
- int main(int argc, string argv [])
- {
- string j = argv [1] ;
- if (argc != 2)
- {
- printf("BAD COMMAND\n");
- return 1;
- }
- for (int s = 0, m = strlen(j); s < m; s++) // to iterate over keyword string
- {
- if ( isalpha (j[s])== false)
- {
- printf("BAD COMMAND\n");
- return 1;
- }
- else
- {
- ;
- }
- }
- char k[strlen(j)] ; // declaring array k
- string p = get_string("plaintext: ");
- char c [strlen(p)]; // a char variable with the same string length of p
- printf("ciphertext: ");
- int x ; // variable to preserve letter case
- int u = 0;
- for (int i = 0, n = strlen(p) ; i < n; i++) //this is the loop to iterate over each character of the string
- {
- k[u] = shift(argv [1][u]);
- if (isalpha (p[i]) == true)
- {
- u++ ;
- }
- if (isupper(p[i]))
- {
- x = 65;
- }
- else if (islower(p[i]))
- {
- x = 97;
- }
- c[i] = (((p[i] - x) + k[u]) % 26) + x ; // this equation is to convert from ascii to alphabet index, as well as plaintext to ciphertext
- printf("%i", k[u]);
- if (isalpha(p[i]))
- {
- printf("%c", c[i]);
- }
- else if (p[i] == 32)
- {
- printf(" "); // to leave spaces as they are
- }
- else
- {
- printf("%c", (p[i])); //to leave non alphaetic characters as they are
- }
- }
- printf("\n");
- }
- int shift(char q) // defining shift function
- {
- int keyword;
- if (islower(q))
- {
- keyword = ((int) (q)) -97;
- }
- else
- {
- keyword = ((int) (q)) -65;
- }
- return keyword;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement