-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvigenere.c
More file actions
84 lines (82 loc) · 1.94 KB
/
vigenere.c
File metadata and controls
84 lines (82 loc) · 1.94 KB
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
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int shift(char c);
int main(int argc, string argv[])
{
char a;
int key, x1;
string text;
//checking the number of arguments in command line
if (argc != 2)
{
printf("Usage: ./vigenere keyword\n");
return 1;
}
//checking for an alphabetic characters
for (int i = 0; i < strlen(argv[1]); i++)
{
a = argv[1][i];
if (isalpha(a) == false)
{
printf("Usage: ./vigenere keyword\n");
return 1;
}
}
//getting string from user
text = get_string("plaintext: ");
printf("ciphertext: ");
//output of encrypted text
for (int i = 0, j = 0, s = strlen(text), s1 = strlen(argv[1]); i < s; i++, j++)
{
//key identification
key = shift(argv[1][j]);
//changing letter identification
x1 = text[i];
// rule for changed letter which is more than "ASCII" aphabetic character
if ((islower(x1) && x1 + key > 'z') || (isupper(x1) && x1 + key > 'Z'))
{
x1 = x1 + key - 26;
printf("%c", x1);
}
//rule for normal alphabetic character
else if (isalpha(x1))
{
printf("%c", x1 + key);
}
//rule for punctuation marks and spaces
else
{
printf("%c", x1);
}
//return the value of the key to the first letter of the word
if (j >= s1 - 1)
{
j = -1;
}
//unchanging key for punctuation marks and spaces
if (ispunct(text[i]) || isspace(text[i]))
{
j--;
}
}
//exit the script
printf("\n");
return 0;
}
// function which defines an integer key from letter
int shift(char c)
{
int c1;
if (islower(c))
{
c1 = c - 97;
}
if (isupper(c))
{
c1 = c - 65;
}
return c1;
}