sting program

You know Galois is a very smart student. He dislikes History exams and you know that he will find a smart way to cheat. You are the supervisor in the exam hall for his History exam and you recover from him a sheet of paper that contains several lines that appear to be in a language not even remotely resembling English even though the alphabets used are from the English language. The Principal will not believe you unless you establish beyond doubt, that the paper recovered from him contains an answer to one of the questions. You show the paper to the head of Computer Science department and he says that it is a well-known crypto method and agrees to decrypt the same for you. It turns out that Galois did the following,

He writes the original message in a zig zag pattern and reads of the lines horizontally. For example, IAMSMARTERTHANYOU is written first as and encoded as IEUATROMRTYSAHNMA

Given the number of rows used by Galois to encode the text and the encoded text, write a program to recover the original text. Note that if the length of the string is not adequate to complete the pattern, Galois pads it with the character “X” to make up the length. These must not appear in the output.
Input Format:

The first line of the input is the number of rows (depth) Galois used in his coding scheme

The next line is the encoded message
Output Format:

The decoded message with all the padding characters (if any) removed.
Constraints:

Depth ≤10

Example 1

Input
5
CCEAEOSNDDEYUEHOT

Output
CANYOUDECODETHESE

Explanation
The depth is 5. When the input message is laid out, it looks like this

Hence the output is CANYOUDECODETHESE

Example 2

Input
5
WTXHUTXAOHXTBIXAS

Output
WHATABOUTTHIS

Explanation
The depth is 5. When the message is laid out, it appears as

The apparent decoded message is WHATABOUTTHISXXXX. But the X’s are padding characters. Hence the output is WHATABOUTTHIS.

Hi,
Using Railfence Cipher algorithm we can solve this…
The following code will help you…
I’m Using Java for this…

int r=depth,len=plainText.length();
int c=len/depth;
char mat[][]=new char[r][c];
int k=0;

String cipherText="";

for(int i=0;i< c;i++)
{
for(int j=0;j< r;j++)
{
if(k!=len)
mat[j][i]=plainText.charAt(k++);
else
mat[j][i]=‘X’;
}
}
for(int i=0;i< r;i++)
{
for(int j=0;j< c;j++)
{
cipherText+=mat[i][j];
}
}
return cipherText;
}

String Decryption(String cipherText,int depth)throws Exception
{
int r=depth,len=cipherText.length();
int c=len/depth;
char mat[][]=new char[r][c];
int k=0;
String plainText="";
for(int i=0;i< r;i++)
{
for(int j=0;j< c;j++)
{
mat[i][j]=cipherText.charAt(k++);
}
}
for(int i=0;i< c;i++)
{
for(int j=0;j< r;j++)
{
plainText+=mat[j][i];
}
}
return plainText;
}
}
class RailFence
{
public static void main(String args[])throws Exception
{
RailFenceBasic rf=new RailFenceBasic();
Scanner scn=new Scanner(System.in);
int depth;
String plainText,cipherText,decryptedText;
System.out.println(“Enter plain text:”);
plainText=scn.nextLine();
System.out.println(“Enter depth for Encryption:”);
depth=scn.nextInt();
cipherText=rf.Encryption(plainText,depth);
System.out.println(“Encrypted text is:\n”+cipherText);
decryptedText=rf.Decryption(cipherText, depth);
System.out.println(“Decrypted text is:\n”+decryptedText);
}
}
OUTPUT

Enter plain text:
railfencecipher
Enter depth for Encryption:
3

Encrypted text is:
rlnchafcieieepr

Decrypted text is:
railfencecipher

can you explain the logic

Hey… I wanted to code this in java but here is the C++ version of the decrypter.
One can also create another module in this program to cipher codes in Railfence Cipher algorithm.

#include <bits/stdc++.h>
using namespace std;
string decryptRailFence(string cipher, int key)
{
char rail[key][cipher.length()];
for (int i=0; i < key; i++)
for (int j=0; j < cipher.length(); j++)
rail[i][j] = ‘\n’;
bool dir_down;
int row = 0, col = 0;
for (int i=0; i < cipher.length(); i++)
{
if (row == 0)
dir_down = true;
if (row == key-1)
dir_down = false;
rail[row][col++] = ‘’;
dir_down?row++ : row–;
}
int index = 0;
for (int i=0; i<key; i++)
for (int j=0; j<cipher.length(); j++)
if (rail[i][j] == '
’ && index<cipher.length())
rail[i][j] = cipher[index++];
string result;
row = 0, col = 0;
for (int i=0; i< cipher.length(); i++)
{
if (row == 0)
dir_down = true;
if (row == key-1)
dir_down = false;
if (rail[row][col] != ‘*’)
result.push_back(rail[row][col++]);
dir_down?row++: row–;
}
return result;
}
int main()
{
cout << decryptRailFence(“CCEAEOSNDDEYUEHOT”, 5) << endl;
cout << decryptRailFence(“WTXHUTXAOHXTBIXAS”, 5) << endl;
return 0;
}

#include<stdio.h>
#include<string.h>
main()
{
int dept;
scanf("%d",&dept);
char a[100];
scanf("%s",a);
char out[100];
int loc;

out[0]=a[0];
int j=2;
int k=0;
int m=2;
int count=0;
out[1]=a[3];
int len=strlen(a);
int ind=0;
while(m–)
{
for(int i=ind;i<len;i++)
{
if(i>3)
{
count++;
if(count==4)
{
out[j]=a[i];
j++;
count=0;
loc=i;
k++;
}
}
if(len-1-loc==3)
{
out[j]=a[i+3];
j++;
count=0;
loc=i+3;
k++;
}
}
count=0;
for(int i=len-1;i>=0;i–)
{
if(len-1==loc)
{
if(count==2)
{
out[j]=a[i];
j++;
count=0;
loc=i;
k++;
}
}
else if(i>=2)
{
if(count==4)
{
out[j]=a[i];
j++;
loc=i;
count=0;
k++;
}
}
else
{
if(count==3)
{
out[j]=a[i];
j++;
count=0;
loc=i;
k++;
}
}
count++;
}
}
for(int m=0;m<j;m++)
if(out[m]!=‘X’)
printf("%c",out[m]);

}