![]() |
![]() ![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]()
![]() Make a program that inputs text from the user. Change the input so that the text is encrypted. Every letter will be substituted with the letter succeeding it in the alphabet and every digit is replaced with the digit higher than it. Z is replaced by A, and 9 is replaced by 0. All other characters will stay the same. Lettercase must be preserved. As an added challenge, you cannot use any IF...THENs, you have to use SELECT CASE with only three branches. Plus, the program must be written in under 15 lines of code. The encrypted text will be displayed afterwards. ![]() Examples:
AaBbCcXxYyZz BbCcDdYyZzAa
9:01 pm 0:12 qn SEAV Softwares TFBW Tpguxbsft Sept. 25, 1998 Tfqu. 36, 2009 ![]() Download the A4 Zip file containing this page plus QBASIC files for you to try. ![]() Go back to the Case Study page. |
![]() |
![]()
|
||||||||||
![]() |
![]() Structuring the program. Before doing anything else, let's first construct the structure of the program. Going by the problem, we could follow this structure:
![]() We used LINE INPUT since the text could have commas in it. We leave the construction of the encrypted string for later since this is where the bulk of the programming is located and needs a more comprehensive discussion. ![]() To build the encrypted string. There are two ways that I can see in building the encrypted string. Which one you use depends on whether you want to retain the original string. The first method is that we build a new string from the original one, thus preserving the original string (just in case you still need it). In the second method, we modify the original string itself, completely replacing its content when the program ends. For both methods, we go through the string on a character-by-character basis, and making the necessary changes to each character (replacing an "A" with a "B", a "g" with an "h", and so on). ![]()
![]() And here is the second method (replacement):
![]() The second method requires that you use the statement form of MID$. The statement form enables us to replace parts of a string without changing the rest of its content or its length. ![]() Encrypting the characters. Now the only thing left is the encryption process. Since we're dealing with text here and changing individual characters based on their order, we may need to use the ASCII functions, ASC and CHR$. If we have a letter contained in a string variable Char$, we can find the next character in the alphabet by simply getting the ASCII number of the character, adding one to it, and obtaining the character with that number. This process can also be applied to numerals.
![]() However, we would have problems if the letter is Z or the number is 9. The next ASCII character to upper-case z is a left square bracket, [, while the characters after lower-case z and 9 are the left curly bracket, {, and the colon, :, respectively. What we can do is to take them as special cases. ![]()
![]() Note that we included the special cases. Also, we used SELECT CASE instead of a bunch of IF...THENs since the problem stated that. ![]()
![]() That's it! ![]() Finally, the completed program! Your final program should look something like the program in Listing A4.1. Here, I used the second method of building the encrypted string since I don't really need the original.
![]() Here's a sample output:
![]() A different algorithm. You'll find on Listing A4.2 another program that answers our problem. This one uses a completely different method of encryption and it also uses integer math instead of string functions, which are notoriously slow. Using integer math is really necessary for speed especially if you decide to use the encryption on a long string, or even whole text files.
![]() Flexible programming. As you have probably noticed by now, there is more than one way of making programs for a problem. Programming is a very flexible discipline, in the sense that it lets you do anything you want. Of course, you should pick the most efficient solution. ![]() ![]() |
||||||||||
![]()
|
|||||||||||
![]() |
![]() Create a program that reverses the process of the original programa decoder. Create a program that uses look-up tables to encrypt text files (make your own encoding/decoding tables). ![]() ![]() |
![]() |
![]() Go back to the Case Study page. Home Page | Program Nook | Instructional | Open Forum Portfolio | Visitor's Area | Connections | About the Site ![]() Copyright © 1997-1999, SEAV Softwares. All rights reserved. Webmaster: Eugene Villar (SEAV); e-mail: evillar@iname.com |