HACKER IN CLASS

QUESTION:- 

A classmate named Pinak has learned ethical hacking and cryptography a few days back. To flaunt his skills he has decided to hack into everyone’s system using the common wifi connectivity used in their college. A hacked system asks for a password that only Pinak has. He sent an email to everyone’s official mail address, which contains a code to crack that is the password to access their system back.
All of your classmates are in deep trouble, as an excellent coder, can you solve their issue by cracking the code for everyone?
All the secret code sent in the email follows a general trend.
1. The first line of the code contains a string P having only characters from A to Z in both uppercase and lowercase combinations as well as numbers formed by digits(0-9). It also contains other symbols like;,-,’,%,&,* etc.
2. The second line contains an integer N <= 1000.
3. The third line has another string Q.
4. You need to concatenate P and Q in order to display the original password.
Conditions : -
  •  You need to rotate the secret code P with respect to N to access the original password. The punctuation marks are non-encrypt-able. They are just needed to have the password working. Means you can not change the position neither the punctuation mark in the string. (For example - N=4, (llz-cvoYs → ppd-gzsCw).
  • You need to rotate the even positions of the string Q with respect to N if Q has an odd length and if it has an even length you need to rotate the odd positions of the string Q. Remember punctuation marks and other symbols (other than alphanumeric) can not be rotated.

INPUT
1. First-line contains an integer T, i.e. number of test cases.
2. Second-line takes a string P.
3. Next line takes an integer N<=1000.
4. Next line takes another string Q.
OUTPUT
The concatenated string of modified P and Q according to the question.
TEST CASES:-
2
Abhij_mmdisfmmnf-#$$56677--
4
Ahdnndjw-awk$%8299
Eflmn_qqhmwjqqrj-#$$90011--Ehhnrdnw-aak$%2239
aBH_etyd789@33$5667-==
4
aBHH_905%^&-AIIiiI
eFL_ixch123@77$9001-==eBLH_945%^&-EIMimI

Ans:-

This question revolves around basic string I/O and manipulation of it as an array. ASCII table is used to convert characters to decimals and again converting them to characters for output. The basic technique is to convert the string elements to decimal and getting them back after modification.

C++ Code :-

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string p_str,q_str;
    int num,t;
    cin >> t;
    while(t--)
    {
    cin >> p_str;
    cin >> num;
    cin >> q_str;
    for(unsigned int i=0;p_str[i]!='\0';i++)
    {
        switch(p_str[i])
        {
        case 'A' ... 'Z' :
            p_str[i] = (((p_str[i]-65)+num)%26)+65;
            break;
        case 'a' ... 'z' :
            p_str[i]=(((p_str[i]-97)+num)%26)+97;
            break;
        case '0' ... '9' :
            p_str[i]=(((p_str[i]-48)+num)%10)+48;
            break;
        default:
            p_str[i]=p_str[i];
        }
    }
   if((q_str.size()-1)%2==0)
    {
    for(unsigned int i=1;i<q_str.size();i=i+2)
        {
            switch(q_str[i])
            {
            case 'A' ... 'Z' :
                q_str[i] = (((q_str[i]-65)+num)%26)+65;
                break;
            case 'a' ... 'z' :
                q_str[i]=(((q_str[i]-97)+num)%26)+97;
                break;
            case '0' ... '9' :
                q_str[i]=(((q_str[i]-48)+num)%10)+48;
                break;
            default:
                q_str[i]=q_str[i];
            }
        }
    }
    else
    {
        for(unsigned int i=0;i<q_str.size();i=i+2)
        {
            switch(q_str[i])
            {
            case 'A' ... 'Z' :
                q_str[i] = (((q_str[i]-65)+num)%26)+65;
                break;
            case 'a' ... 'z' :
                q_str[i]=(((q_str[i]-97)+num)%26)+97;
                break;
            case '0' ... '9' :
                q_str[i]=(((q_str[i]-48)+num)%10)+48;
                break;
            default:
                q_str[i]=q_str[i];
            }
        }
    }
    cout << p_str + q_str << endl;
    }
    return 0;
}
Hope that helps, keep Coding :)

Comments