All pastes #203675 Raw Edit

G0SUB

public c v1 · immutable
#203675 ·published 2006-10-15 09:41 UTC
rendered paste body
/* -*- compile-command: "gcc -g rail.c -o rail -lm" -*- *//** * rail.c -- Simple transposition cipher * Time-stamp: "2006-10-15 15:09:11 ghoseb" */#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <math.h>#define MESSAGE "WEAREDISCOVEREDFLEEATONCE"int main(int argc, char *argv[]){        int len = strlen(MESSAGE);        int row = 3;        int column = abs(ceil (len/(float)row));        int i, j, k;        int index = 0;        char **fence;                char *str = (char *)malloc(sizeof(MESSAGE));/*         char *cipher = (char *)malloc(sizeof(MESSAGE)); */                strcpy(str, MESSAGE);        for (i=0; i<row; i++) {                fence[i] = (char *)malloc(column * sizeof(char));                for (j=0; j<column; j++) {                        index = i+(row*j);                        if (index < len) {                                fence[i][j] = str[index];                        } else {                                fence[i][j] = 'X';                        }                }        }        /*          str now contains the cipher text        */        k = 0;        for (i=0; i<row; i++) {                for (j=0; j<column; j++) {                        str[k] = fence[i][j];                        k++;                }        }        printf ("%s\n", str);        free (str);        return 0;}