-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrailfence.c
191 lines (132 loc) · 3.13 KB
/
railfence.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* railfence.c
*
* Created on: 15-Dec-2015
* Author: Meena
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
The Rail Fence cipher is a form of transposition cipher that gets its name
from the way in which it is encoded. In the rail fence cipher, the plaintext
is written downwards on successive "rails" of an imaginary fence, then moving up
when we get to the bottom. The message is then read off in rows.
For example, using three "rails" and a message of 'WE ARE DISCOVERED. FLEE AT ONCE',
the cipherer writes out:
W . . . E . . . C . . . R . . . L . . . T . . . E
. E . R . D . S . O . E . E . F . E . A . O . C .
. . A . . . I . . . V . . . D . . . E . . . N . .
Then reads off:
WECRL TEERD SOEEF EAOCA IVDEN
(The cipherer has broken this ciphertext up into blocks of five to help avoid errors.)
*/
/*generic flow
x rows y columns
row is always incrementing
i j
i+1 j+1
i+2 j+2
i+3 j+3
....j+x-1
......
i+y-1
*/
/*
logic/algorithm
two dimensional array
for the above example
i j
i+1 j+1
i+2 j+2
i+3 j+1
i+4 j
i+5 j+1
i+6 j+2
i+7 j+1
......
The no of rows and columns specified by user is wrong approach
Instead no of rails(rows) should be taken as input and columns
should be dynamically determined based on strlen
*/
#define MAXDIM 50
main() {
char pltxt[100];
char test[10]={NULL};
int i;
int row;
int col;
int j;
int lin;
int ideal_len;
int traverse;
int dirfl; /*to help in determining whether to increment or decrement*/
char matrix[MAXDIM][MAXDIM]; //how to declare a 2-d array?? :(
printf("Enter a string under 100 chars, avoid special characters");
fflush(stdout);
gets(pltxt);
printf("The input statement is %s", pltxt);
printf("\n");
ideal_len=strlen(pltxt)/4;
printf("Input the no. of rails, let it be less than %d in length:", ideal_len);
fflush(stdout);
scanf("%d", &row);
printf("no. of rows is %d\n", row);
lin=0;
printf("\n");
puts(pltxt);
for(i=0;i<row;i++)
{
for(j=0;j<strlen(pltxt);j++)
{
matrix[i][j]='^';
}
}
printf("the num of rows and cols were %d and %d", i, j);
printf("\n");
puts(pltxt);
i=0; j=0;
dirfl=0;
while(pltxt[lin]!='\0')
{
matrix[i][j] = pltxt[lin]; //is there a way to mark these positions of i and j?
printf("matrix[%d][%d]=%c\n", i,j,matrix[i][j]);
/*
*/
if(!dirfl)
i++;
else
i--;
if(i==row)
{
dirfl=1;
i--;
i--;
}
if(i==0)
dirfl=0;
lin++;
j++;
// printf("%d, %c", j, pltxt[lin]);
}
/*printf("the num of rows and cols were %d and %d", row, j);
printf("\n");
puts(pltxt);*/
for(i=0;i<row;i++)
{
for(j=0;j<strlen(pltxt);j++)
{
printf("%4c",matrix[i][j]);
}
printf("\n");
}
printf("\n\n\n");
/*
for rail fence text
and decipher rail fence text
*/
for(i=0;i<10;i++)
{
printf("%4c",test[i]);
}
}