1 Replies - 10803 Views - Last Post: 30 November 2010 - 01:31 AM Rate Topic: -----

#1 taylorlg89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-November 10

Substitution Cipher

Posted 29 November 2010 - 08:02 PM

For a school assignment I am required to make a program that will encrypt or decrypt a message. We were given the table of subsitutions. I have it store in a struct

   struct database //define struct 
	   char normal;
	   char substitute;

Then I declared a record of arrays

database table [30];

Then I have the substitution table stored in table.normal with the original character and table.substitute with the substitution character.

table[1].normal = 'a';
table[1].substitute = '*';

table[2].normal = 'b';
table[2].substitute = '=';

I have to prompt the user whether they want to encrypt and decrypt the message (this I know how to do)

I am required to use the following code to get text from the user and to write it back to the console
// readLine()
// Uses cin in a slightly different way to allow blanks to be read
// The line "ends" when the user types "return" i.e., '\n'
// but if the line is too long, the extra characters are ignored
// User must provide the array, and maximum size,
// The function returns the number of characters actually read
// can be less than the maximum
// Bug fix: this version really does get rid of extra characters!

int readLine(char theLine[], int maxChars)
  string buffer;
  getline(cin, buffer);  
  int charCount; 
  for(charCount = 0; charCount < maxChars && charCount < buffer.length();  charCount = charCount + 1)
    theLine[charCount] = buffer[charCount];  // copy to the array

  return charCount;

// writeLine()
// uses cout to output a char array
// (Note: this use of theLine is not standard)
void writeLine(char theLine[], int lineLength)
  int index;
  for (index = 0; index < lineLength; index++)
    cout << theLine[index];
  cout << endl;

So far I have prompted the user to enter a message to encrypt, and whether they want to encrypt it or decrypt it. What I am having with is how to do a linear search. I have the user input stored in an array. What I think I need to do is go to the first element of the array, and go through the records to find the substitution character and put it into a second array. Once I have converted all the characters and filled the new array. Display the contents of the second array. However, I am not sure how to implement this in C++. Can anyone give me any pushes in the right direction?

Is This A Good Question/Topic? 0
  • +

Replies To: Substitution Cipher

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Substitution Cipher

Posted 30 November 2010 - 01:31 AM

Your solution will work and is easy to write.

Give it a try and show us your best effort (in a compiling program) if you have problems.

Do it in stages.

First stage write a loop that walks through every element of the plaintext string and outputs each character to the screen.
Then nest a loop that uses the current plain text character to find the substitute value.
Continue on in small steps getting each small step to work before trying the next small step.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1