-
Notifications
You must be signed in to change notification settings - Fork 0
/
vigenere.c
70 lines (60 loc) · 1.65 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
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
string key = argv[1];
string line = NULL;
int endChar = 0;
int counter = 0;
if(argc != 2)
{
printf("Usage: ./asciimath key\n");
return 1;
}
for (int n = 0; n < strlen(key); n++)
{
if (!isalpha(argv[1][n]))
{
printf("enter an aplhebetical char\n");
return 1;
}
}
line = GetString();
for (int i = 0, j = strlen(line); i < j; i++)
{
if (isalpha(line[i]))
{
counter = (endChar % strlen(key));
}
if (islower(line[i]))
{
if (islower(key[counter]))
{
line[i] = ((line[i] - 'a' + key[counter] - 97) %26) + 97;
}
else
{
line[i] = ((line[i] - 'a' + key[counter] - 65) % 26) + 97;
printf("%c", line[i]);
}
}
if (isupper(line[i]))
{
if (islower(key[endChar]))
{
line[i] = ((line[i] - 'A' + key[counter] - 97) % 26) + 65;
printf("%c", line[i]);
}
else
{
line[i] = ((line[i] - 'A' + key[counter] - 65) % 26) + 65;
printf("%c", line[i]);
}
}
printf("%c", line[i]);
}
printf("\n");
}