X and Y are best friends and they love to chat with each other. But their recent concerns about the privacy of their messages have distant them. So they decided to encrypt their messages with a key, K, such that the character of their messages is now shifted K times towards the right of their initial value. Their techniques only convert numbers and alphabets while leaving special characters as it is.
Provided the value K you are required to encrypt the messages using their idea of encryption.
Example:
Input: n = 12, K = 4 , s = "Hello-World!"
Output: Lipps-Asvph!
Approach
C++
#include <bits/stdc++.h>using namespace std;string secretMessage(int n, int K, string s){int i = 0;while (i < s.size()){if (s[i] >= 'A' && s[i] <= 'Z')s[i] = 'A' + (s[i] - 'A' + K) % 26;else if (s[i] >= 'a' && s[i] <= 'z')s[i] = 'a' + (s[i] - 'a' + K) % 26;else if (s[i] >= '0' && s[i] <= '9')s[i] = '0' + (s[i] - '0' + K) % 10;i++;}return s;}int main(){int n = 12, K = 4;string s = "Hello-World!";cout << secretMessage(n, K, s) << "\n";return 0;}
No comments:
Post a Comment