Search This Blog

Tuesday 2 February 2016

Rail-Fence Cipher Program

#include<iostream>
#include<string.h>
#include<math.h>

using namespace std;

const int size=1000;

class Matrix
{
int d,l;
char a[size][size];
public:
Matrix(char[]);
void display();
};

Matrix::Matrix(char x[])
{
l=strlen(x);
int flag=1;
cout << "\t\tEnter depth of the text:";
cin >> d;
int j=0;
for(int i=1;i<=l;i++)
{
int flag2=0;
for(int k=0;k<d;k++)
{
if(j==k && flag2==0)
{
a[j][i-1]=x[i-1];
if(i%d==0)
flag=flag*-1;
j+=flag;
if(j<0)
j=1;
flag2=1;
}
else
a[k][i-1]='$';
}
}
}

void Matrix::display()
{
for(int i=0;i<d;i++)
{
for(int j=0;j<l;j++)
{
if(a[i][j]!='$')
cout << a[i][j];
}
}
}

int main()
{
char a[1000];
cout << "\t\tEnter string:";
cin >> a;
Matrix c(a);
c.display();
return 0;
}

enter string :
abcdef

a        e
  b   d    f
    c
enter depth :
3

aebdfc

No comments:

Post a Comment