-
Notifications
You must be signed in to change notification settings - Fork 2
/
vigenere.c
83 lines (66 loc) · 1.33 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
/****************************************************************************
* vigenere.c
*
*
* Converts plain text to cipher text using vigenere chipher
*
* by Bhavika
***************************************************************************/
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <ctype.h>
int
main(int argc, char* argv[])
{
int i=0,j=0,l=0;
if(argc!= 2)
{
printf("\aTry again by passing single arguement");
return 1;
}
else
{
for(int m=0;argv[1][m]!='\0';m++)
{
if(!isalpha(argv[1][m]))
{
printf("\aTry again by passing alphabetical word as an arguement\n");
return 1;
}
}
}
//Read string
printf("Enter the string");
char s[256];
fgets(s, sizeof(s), stdin);
char* k=argv[1];
int len=strlen(k);
//for converting every key to lower case
while(k[l]!='\0')
{
if(isupper(k[l]))
k[l]= tolower(k[l]);
l++;
}
//changes each plain text letter to ciphr text
while(s[i]!='\0')
{
if((int)s[i]>=65 && (int)s[i]<=90)
{
s[i]='A'+(((s[i]-65)+(k[j]-97))%26);
j++;
}
else if((int)s[i]>=97 && (int)s[i]<=122)
{
s[i]='a'+(((s[i]-97)+(k[j]-97))%26);
j++;
}
if(j==len)
j=0;
i++;
}
//show the result
printf("%s\n",s);
return 0;
}