Saturday, September 2, 2017

Message Encryption

To encrypt messages Jil will first decide on the number of columns C to use. Then Jil will pad the message with letters chosen randomly so that they form a rectangular matrix. Finally Jil will write down the message navigating the rows from left to right and then from right to left.The program must accept the encrypted message M as input and then extract and print the original message (along with any additional padding letters) from the encrypted one based on the value of C. 

Input Format: 
First line will contain the string value of the encrypted message M. Second line will contain the integer value of the column used for the encryption. 

Output Format: 
First line will contain the string value of the original message (along with any additional padding letters)

Sample Input/Output: 
Input: 
midinadiazne 
Output: 
madeinindiaz 
Explanation:
m i d 
a n i 
d i a 
e n z 
Here z is the padding letter. The navigating across the rows mid (left to right) ina (right to left) and so on we come up with the encrypted message midinadiazne. 

Solution: 
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
 string s;
 int i,n,j,x,c=0;
 cin>>s;
 cin>>n;
 x=s.size()/n;
 for(i=0;i<n;i++)
 for(j=0;j<x;j++)
 {
    if(j%2==0)
    cout<<s[i+j*n];
    else
    cout<<s[i+((j+1)*n)-(2*i)-1];
 }
}



Share: