Indian army is going to do a surprise attack on one of its enemy's country. The President of India, the Supreme Commander of the Indian Army will be sending an alert message to all its commanding centers. As the enemy would be monitoring the message, the Indian army is going to encrypt(cipher) the message using a 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.
Example:
Input: s = "All-convoYs-9-be:Alert1." , k = 4
Output: Epp-gsrzsCw-3-fi:Epivx5.
Approach
C++
#include <bits/stdc++.h>using namespace std;string chipher(string s, int k){int n = s.size();map<int, char> mp, mp1, mp3;for (int i = 0; i < 26; i++)mp[i] = i + 'A';for (int i = 0; i < 26; i++)mp1[i] = i + 'a';for (int i = 0; i < 10; i++)mp3[i] = i + '0';for (int i = 0; i < n; i++){if (s[i] >= 'A' && s[i] <= 'Z'){int x = (s[i] - 'A' + k) % 26;s[i] = mp[x];}else if (s[i] >= 'a' && s[i] <= 'z'){int x = (s[i] - 'a' + k) % 26;s[i] = mp1[x];}else if (s[i] >= '0' && s[i] <= '9'){int x = (s[i] - '0' + k) % 10;s[i] = mp3[x];}}return s;}int main(){string s = "All-convoYs-9-be:Alert1.";int k = 4;cout << chipher(s, k) << "\n";return 0;}
No comments:
Post a Comment