226k views
3 votes
Language: C

Introduction
For this assignment you will write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the text. The first number denotes the position of a word in the key text (starting at 0), and the second number denotes the position of the letter in the word (also starting at 0). For instance, given the following key text (the numbers correspond to the index of the first word in the line):
[0] 'Twas brillig, and the slithy toves Did gyre and gimble in the wabe;
[13] All mimsy were the borogoves, And the mome raths outgrabe.
[23] "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
[36] Beware the Jubjub bird, and shun The frumious Bandersnatch!"
[45] He took his vorpal sword in hand: Long time the manxome foe he sought—
The word "computer" can be encoded with the following pairs of numbers:
35,0 catch
5,1 toves
42,3 frumious
48,3 vorpal
22,1 outgrabe
34,3 that
23,5 Beware
7,2 gyre

User Aerion
by
4.9k points

1 Answer

4 votes

The .cpp code is avaiable bellow

Code:

#include <stdio.h>

#include <string.h>

int main()

{

char **kW;

char *fN;

int nW = 0;

kW = malloc(5000 * sizeof(char*));

for (int i = 0; i<5000; i++)

{

kW[i] = (char *)malloc(15);

}

fN = (char*)malloc(25);

int choice;

while (1)

{

printf("1) File text to use as cipher\\");

printf("2) Make a cipher with the input text file and save result as output file\\");

printf("3) Decode existing cipher\\");

printf("4) Exit.\\");

printf("Enter choice: ");

scanf("%d", &choice);

if (choice == 1)

{

nW = readFile(kW, fN);

}

else if (choice == 2)

{

encode(kW, fN, nW);

}

else

{

Exit();

}

printf("\\");

}

return 0;

}

int readFile(char** words, char *fN)

{

FILE *read;

printf("Enter the name of a cipher text file:");

scanf("%s", fN);

read = fopen(fN, "r");

if (read == NULL)

{

puts("Error: Couldn't open file");

fN = NULL;

return;

}

char line[1000];

int word = 0;

while (fgets(line, sizeof line, read) != NULL)

{

int i = 0;

int j = 0;

while (line[i] != '\0')

{

if (line[i] != ' ')

{

if (line[i] >= 65 && line[i] <= 90)

{

words[word][j] = line[i]; +32;

}

else

{

words[word][j] = line[i];

}

j++;

}

else

{

words[word][j] = '\\';

j = 0;

word++;

}

i++;

}

words[word][j] = '\\';

word++;

}

return word;

}

void encode(char** words, char *fN, int nwords)

{

char line[50];

char result[100];

if (strcmp(fN, "") == 0)

{

nwords = readFile(words, fN);

}

getchar();

printf("Enter a secret message(and press enter): ");

gets(line);

int i = 0, j = 0;

int w = 0, k = 0;

while (line[i] != '\0')

{

if (line[i] >= 65 && line[i] <= 90)

{

line[i] = line[i] + 32;

}

w = 0;

int found = 0;

while (w<nwords)

{

j = 0;

while (words[w][j] != '\0')

{

if (line[i] == words[w][j])

{

printf("%c -> %d,%d \\", line[i], w, j);

found = 1;

break;

}

j++;

}

if (found == 1)

break;

w++;

}

i++;

}

result[k] = '\\';

}

void Exit()

{

exit(0);

}

User Daniel Aagentah
by
5.8k points