Some guidance needed

Can someone please help me come up with a pseudo code for this question. I can't seem to figure out an algorithm that makes sense.

A program that reads text from a file and encrypts it by using a simple technique. The encryption is done simply by replacing each character in the file with its successor. For instance, “a” is replaced with “b” or “c” is replaced with “d”. A “z” should be replaced with an “a”. The program must be able to handle both lowercase and capital letters. It should also be able to decrypt the encrypted file by replacing each character of the encrypted file with its predecessor. For instance, “b” is replaced with “a” or “d” is replaced with “c”. An “a” should be replaced with a “z”. The program should give the user a choice between encrypting or decrypting a file. If the user chooses either of the options, the program should allow the user to specify the names of the input and output files, and then do the process according to the option chosen by the user. It should then save the result to the specified output file name. Display both the input file and the output file on the screen after the processing has been done.

Hello,

Should it maybe begin like

Ask user for name for new encrypted file

Open 2 filestreams (one with the name of the new user gave)
declare a string

Start for loop
Read test from file
Extract text from filestream as a string
close for loop

Start for loop for reading of string
Find a
Replace with b

Etc
Read through the assignment and start writing prototypes for the functions/code that you'll need. When you're done, re-read the assignment to see if you've covered everything. Make changes as needed. Repeat until you have a pretty good outline of the program. Then just fill in the code!

A program that reads text from a file and encrypts it by using a simple technique. The encryption is done simply by replacing each character in the file with its successor.

so you''ll need an encryption function. It encrypts one character at a time so
char encrypt(char);
For instance, “a” is replaced with “b” or “c” is replaced with “d”. A “z” should be replaced with an “a”. The program must be able to handle both lowercase and capital letters.

Awesome. Just put that in a comment inside encrypt:
1
2
3
4
5
char encrypt(char)
{
//“a” is replaced with “b” or “c” is replaced with “d”. A “z” should be replaced
// with an “a”. The program must be able to handle both lowercase and capital letters.
}

It should also be able to decrypt the encrypted file...

So we also need
char decrypt(char);
The program should give the user a choice between encrypting or decrypting a file.

Let's put that in main():
1
2
3
int main()
{
    //prompt for encrypt or decrypt 

the program should allow the user to specify the names of the input and output files,

Put that in main too
// prompt for input and output files
then do the process according to the option chosen by the user.

Hmm. Let's think about this a little. So we'll need to open the input file, encrypt or decrypt, and write the result to the output. I'm not sure what the arguments should be so for now let's just call that:
1
2
3
4
5
6
7
8
void process(infile, outfile, encryptOrDecrypt)
{
// open infile
// open outfile
// for each char in infile
    // encrypt or decrypt
    // write to outfile
}


Display both the input file and the output file on the screen after the processing has been done.

So we also need
1
2
3
4
displayFile(theFile)
{
   // display the file on the screen
}

Now go back through it. Work on more details of what happens in main() and what the parameters are for the functions that don't have them fully specified. Resist the urge to write code. At this point your goal is a solid definition of the functions, what they do, and what their parameters are.

When you're done you'll have a pretty design and well documented code... minus the code! Now go back and start writing the code itself. You'll probably find that it goes very quickly.
Hello nomat,

To go along with what dhayden said I find that breaking up the specs something like this is helpful:

A program that reads text from a file and encrypts it by using a simple technique.

The encryption is done simply by replacing each character in the file with its successor. For instance, “a” is
replaced with “b” or “c” is replaced with “d”. A “z” should be replaced with an “a”.

The program must be able to handle both lowercase and capital letters.

It should also be able to decrypt the encrypted file by replacing each character of the encrypted file with its
predecessor. For instance, “b” is replaced with “a” or “d” is replaced with “c”. An “a” should be replaced with
a “z”.

The program should give the user a choice between encrypting or decrypting a file.If the user chooses either of
the options, the program should allow the user to specify the names of the input and output files, and then do
the process according to the option chosen by the user.

It should then save the result to the specified output file name.

Display both the input file and the output file on the screen after the processing has been done.


Hope that helps,

Andy
Guys thanks for the feedback. I'm currently trying out a few items based on your suggestions. Will show you the code when I'm done.

Regards,
a really snarky way to do this (for ascii, unicode isnt possible) is to just make an array of 256 chars and assign what you want changed, then do a lookup...

looks like

newval = lookup[oldval] or for c-strings
newval[i] = lookup[oldval[i]];

where for example lookup['a'] is 'z' or whatever.

its faster, and it avoids all the convoluted logic. but you have to build the table, which you can do in a throw-away program that writes the correct values in a char lookup[256] = {...} type statement.

You probably should do the code with the logic once to understand it, but the table is how I would do it "commercially".
Last edited on
Topic archived. No new replies allowed.