0% found this document useful (0 votes)
43 views2 pages

Rail Fence.

The document contains a C program that encrypts a secret message using a rail fence cipher method. It prompts the user to enter a message and the number of rails, then organizes the characters in a zigzag pattern before printing the encrypted text. The program initializes a matrix to store the characters and handles the input and output accordingly.

Uploaded by

ytube79120
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

Rail Fence.

The document contains a C program that encrypts a secret message using a rail fence cipher method. It prompts the user to enter a message and the number of rails, then organizes the characters in a zigzag pattern before printing the encrypted text. The program initializes a matrix to store the characters and handles the input and output accordingly.

Uploaded by

ytube79120
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#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;

You might also like