Sunteți pe pagina 1din 3

TUTOTIRAL-4

1)
AIM :- To implement simple Columnar
Transposition Technique
2)

Source Code :-

#include<iostream.h>
#include<string.h>
#include<conio.h>
void main()
{
char pt[100], key[35], ct[100], col[100];
int ptLen = 0,kLen,cnt;
int iArray[20]
={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
clrscr();
cout << "\n Enter String(without space) : ";
cin >> pt;
ptLen = strlen(pt);
pt[ptLen + 1] = '\0';
cout << "\n Enter the key: ";
cin >> key;
kLen = strlen(key);
key[kLen + 1] = '\0';
strcpy(col, key);
col[kLen + 1] = '\0';
cnt = 0;
//cout << strOriginalIput << "\n";
for (int i = 0; i < kLen - 1; i++)
{
for (int j = 0; j < kLen - 1 - i; j++)
if (key[j + 1] < key[j])
{
/* compare the two neighbors */
char tmp = key[j]; /* swap a[j] and a[j+1] */
key[j] = key[j + 1];
key[j + 1] = tmp;
int t = iArray[j];
iArray[j] = iArray[j + 1];
iArray[j + 1] = t;
}
120570107017

Page 1

}
cnt = 0;
for (int z = 0; z < kLen; z++)
{
for (int x = 0; x <= kLen; x++)
{
if ((iArray[z] + kLen * x) <= ptLen)
{
ct[cnt++] = pt[(iArray[z] + kLen * x) - 1];
}
}
}
ct[ptLen] = '\0';
int nl = 1;
for (i = 0; i < kLen; i++)
cout << col[i] << " ";
cout << "\n-------------------------------";
cout << "\n";
for (i = 0; i < ptLen; i++)
{
if (i == kLen * nl)
{
cout << "\n" << pt[i] << " ";
nl++;
}
else
cout << pt[i] << " ";
}
cout << "\n\n" << "Encrypted String : " << ct;
cout << "\n";
char strtmp[100];
cnt = 0;
for (z = 0; z < kLen; z++)
{
for (int x = 0; x <= kLen; x++)
{
if ((iArray[z] + kLen * x) <= (ptLen))
strtmp[iArray[z] + (kLen * x) - 1] = ct[cnt++];
}
}
strtmp[ptLen] = '\0';
cout <<"\n\n"<<"Decrypted String : " << strtmp << "\n\n";
getch();
120570107017

Page 2

3)

Output :-

120570107017

Page 3

S-ar putea să vă placă și