forked from imdhanish/HackerEarth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCipher.cpp
More file actions
65 lines (57 loc) · 1.81 KB
/
Cipher.cpp
File metadata and controls
65 lines (57 loc) · 1.81 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
//Questions
/*
Indian army is going to do a surprise attack on one of its enemies country. The President of India, the Supreme Commander of the Indian Army will be sending an alert message to all its commanding centers.
As enemy would be monitoring the message, Indian army is going to encrypt(cipher) the message using basic encryption technique. A decoding key 'K' (number) would be sent secretly.
You are assigned to develop a cipher program to encrypt the message. Your cipher must rotate every character in the message by a fixed number making it unreadable by enemies.
Given a single line of string 'S' containing alpha, numeric and symbols, followed by a number '0<=N<=1000'. Encrypt and print the resulting string.
Note: The cipher only encrypts Alpha and Numeric. (A-Z, a-z, and 0-9) . All Symbols, such as - , ; %, remain unencrypted.
*/
//Sample Input
/*
All-convoYs-9-be:Alert1.
4
*/
//Sample Output
/*
Epp-gsrzsCw-3-fi:Epivx5.
*/
//Explanation
/*
First line contains the string to convert. S
Second line contains the number, encrypt key. K
A becomes E, Y becomes C, 9 becomes 3,
-, . unchanged.
*/
//Solution
#include <bits/stdc++.h>
using namespace std;
int main()
{
char s[1000];
int k;
cin>>s>>k;
for(int i=0;i<strlen(s);i++)
{
if(isalnum(s[i])){
if(isalpha(s[i])){
if(isupper(s[i])){
s[i]=s[i]+k%26;
if(int(s[i]>90)){
s[i]=s[i]-26;
}
}else{
s[i]=s[i]+k%26;
if(int(s[i])>122){
s[i]=s[i]-26;
}
}
}else{
s[i]=s[i]+k%10;
if(int(s[i])>57){
s[i]=s[i]-10;
}
}
}
}
cout<<s;
}