#include <stdio.
h>
#include <string.h>
#include <stdlib.h>
int main() {
int i, j, len, rails, count;
int code[100][100] = {0}; // Initialize the matrix with 0
char str[1000];
printf("\nEnter a Secret Message: ");
fgets(str, sizeof(str), stdin);
// Remove newline character if present
str[strcspn(str, "\n")] = 0;
len = strlen(str);
printf("\n\nEnter number of rails: ");
scanf("%d", &rails);
count = 0;
j = 0;
while (j < len) {
if (count % 2 == 0) {
for (i = 0; i < rails && j < len; i++) {
code[i][j] = (int)str[j];
j++;
} else {
for (i = rails - 2; i > 0 && j < len; i--) {
code[i][j] = (int)str[j];
j++;
count++;
printf("\nEncrypted Text: ");
for (i = 0; i < rails; i++) {
for (j = 0; j < len; j++) {
if (code[i][j] != 0)
printf("%c", code[i][j]);
printf("\n");
return 0;