/
vigenere.c
98 lines (81 loc) · 2.31 KB
/
vigenere.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// program to encrypt user string by keyword entered in command line
int main(int argc, string argv[])
{
// declare variables
int shift;
int KeyValue;
// only accept two command line arguments
if (argc != 2)
{
printf("You screwed up!!!\n");
return 1;
}
// capture the second command line argument as a string
string key = argv[1];
// iterate through the key to make sure it's alphabetical
for (int n = 0, keylength = strlen(argv[1]); n < keylength; n++)
{
if ((key[n] >= '0') && (key[n] <= '9'))
{
printf("Sorry!\n");
return 1;
}
}
// get the plain text
string PlainText = GetString();
// iterating through plaintext
for(int i = 0, j = 0, length = strlen(PlainText); i < length; i++, j++)
{
// start the key again if key shorter than PlainText
if (j >= strlen(key))
{
j = 0;
}
// declared a variable of a different name
KeyValue = key[j];
// skip key[j] if PlainText[i] is not an alpha character
if (!isalpha(PlainText[i]))
{
j = (j - 1);
}
// makes Aa = 0, Zz = 25 for the uppercase letters
if ((KeyValue >= 'A') && (KeyValue <= 'Z'))
{
KeyValue = (KeyValue - 'A');
}
// makes Aa = 0, Zz = 25 for the lowercase letters
if ((KeyValue >= 'a') && (KeyValue <= 'z'))
{
KeyValue = (KeyValue - 'a');
}
// encryption
shift = (PlainText[i] + KeyValue);
// wrapping after Z for uppercase letters
if (isupper(PlainText[i]) && (shift > 'Z'))
{
shift = (shift - 26);
}
// wrapping after z for lowercase letters
if (islower(PlainText[i]) && (shift > 'z'))
{
shift = (shift - 26);
}
// if character is alphabetical, print encrypted result
if (isalpha(PlainText[i]))
{
printf("%c", shift);
}
// if non-alphabetical character, print as is
else
{
printf("%c", PlainText[i]);
}
}
printf("\n");
return 0;
}