-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.cpp
More file actions
58 lines (52 loc) · 1.05 KB
/
cipher.cpp
File metadata and controls
58 lines (52 loc) · 1.05 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
#include <bits/stdc++.h>
#include<string.h>
#include <stdio.h>
using namespace std;
void encrypt(int k,char a[50]){
int length=strlen(a);
char b[length];
for(int i=0;i<length;i++)
if(isalpha(a[i]))
{
if(isupper(a[i]))
b[i]=(char)((k+(int)a[i]-65)%26+65);
else
b[i]=(char)((k+(int)a[i]-97)%26+97);
}
else
b[i]=a[i];
cout<<b<<endl;
}
void decrypt(int k,char a[50])
{
int length=strlen(a);
char b[length];
for(int i=0;i<length;i++)
if(isalpha(a[i]))
{
if(isupper(a[i]))
b[i]=(char)((26-k+(int)a[i]-65)%26+65);
else
b[i]=(char)((26-k+(int)a[i]-97)%26+97);
}
else
b[i]=a[i];
cout<<b<<endl;
}
int main()
{
int n,k,i;
char a[50];
cin>>n>>k;
for( i=0;i<n;i++)
{
cin.getline(a,50,'/n');
encrypt(k,a);
}
for( i=0;i<n;i++)
{
cin.getline(a,50,'/n');
decrypt(k,a);
}
return 0;
}