My Book Cipher

Hey guys. I'm looking for a hand programming a Book Cipher. This is similar to a post I read about Book Ciphers on this site, but I'm looking for more direct help.I have to create two programs: one that encodes a file using a book cipher, and one the decodes it. I need to replace a message with a number representing each characters placement within a text file. The command line input need to be in this order "bcencode.exe bookfile messagefile codedfile". I hardly know where to start any help would be greatly appreciated.
Thanks.
Last edited on
First I'd start with a bit of design. A 'Book' class could be appropriate:
1
2
3
4
5
6
7
8
9
class Book
{
private:
	map<char, int> myMap;

public:
	void PopulateMap(char * filename);
	int GetValueForChar(char key);
};


The 'myMap' member would be your associative array of characters to positions in your bookfile. This map could be populated in the 'PopulateMap' method. This method might set up a stream to read the bookfile, and populate the map with the chars/position pairs that it reads:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	char s;
	int i=0;
	// Open stream
	ifstream myfile ("example.txt");	
	if (myfile.is_open())
	{
		while (! myfile.eof() )
		{
			// Get the next character and store it 
			// in the map with the position value
			myfile.get(s);
			myMap[s] = i++;
		}
		myfile.close();
	}


Once your Book object is set up, encoding a file would simply involve reading chars from the messagefile, looking up the corresponding ints using the 'GetValueForChar' method, and outputting these integers (perhaps spaced or something) to your codedfile.

The decoding process would involve something similar. In the decoding program, the map in the Book class would map ints to chars, but it could still be populated using a similar approach in the 'PopulateMap' method.

There's likely a neater way to do this, but it's a start.

Don't forget to #include the relevant headers (i.e. iostream, fstream, map).

Hope that helps.
Topic archived. No new replies allowed.