-
-
Save anonymous/c7652d36e1356dec14d66ab2bab19638 to your computer and use it in GitHub Desktop.
vigenere.c - shared from CS50 IDE
This file contains 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> | |
#include <cs50.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <ctype.h> | |
int c; | |
int k1; | |
int p1; | |
int main(int argc, string argv[]) { | |
if (argc != 2) { | |
printf ("ERROR\n"); | |
return 1; | |
} | |
for (int i= 0, n = strlen(argv[1]); i < n; i++) { | |
if (!isalpha (argv[1][i])) { | |
printf ("ERROR\n"); | |
return 1; | |
} | |
} | |
printf("plaintext:"); | |
string p = GetString(); | |
printf ("ciphertext:"); | |
for (int i = 0, n = strlen(p); i < n; i++) { //beginning for loop | |
if (isupper (argv[1][i])) { //transforming iterated "key" elements in alphabetical order | |
k1 = argv[1][i] - 65; | |
} | |
else if (islower (argv[1][i])) { | |
k1 = argv[1][i] - 97; | |
} | |
if (isalpha (p[i])) { //adding alphabetically ordered letter and then convert them i ASCII encrypted values | |
if (isupper (p[i])) { | |
p1 = p[i] - 65; | |
c = (p1 + k1)%26 + 65; | |
printf ("%c", c); | |
} | |
else if (islower (p[i])) { | |
p1 = p [i] - 97; | |
c = (p1 + k1)%26 + 97; | |
printf ("%c", c); | |
} | |
} | |
if (isdigit (p[i])) { | |
c = p[i]; | |
printf ("%c", c); | |
} | |
if (ispunct (p[i])) { | |
c = p[i]; | |
printf ("%c", c); | |
} | |
if (isblank (p[i])) { | |
c = p[i]; | |
printf ("%c", c); | |
} | |
} | |
printf ("\n"); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment