In principle substituting one letter for another is

This preview shows page 3 - 5 out of 7 pages.

In principle, substituting one letter for another is straightforward. It is best accomplished by a table look up, where table means array in C. This operation is made easier by the way C treats characters as just very small integers. Imagine the following array Subscript 0 1 2 3 4 5 Element Value A B C D E F You might declare it like this: char lookupTable[6] = { 'A', 'B', 'C', 'D', 'E', 'F' } ; Now to assign the 3 rd (counting from zero) element to a variable, I could write: char myChar = lookupTable[2] ; and myChar would contain 'C'. So far so good. But what if I wanted to translate (look up) a value as follows: I want 'U” to map to 'A', 'V' to 'B', 'W' to 'C', 'X' to 'D' and so on. How could I code this easily? I know that I could have seven if statements or a switch with seven cases , but that's going to be intractably unwieldy for all the letters of the alphabet (times the number of rotors needed). The key to the answer is to understand that not only is 'U' an ASCII character , it is also a number between -127 and 127, specifically U equals 85. ( )
Image of page 3

Subscribe to view the full document.

So if I had the whole ASCII table in the array named lookupTable -- well, the 127 positive values, anyway -- I could just get element number 85 directly. ,I'd be all set because I could write: char myCharForU = lookupTable[85] ; printf ( “U=%c\n”, myCharForU ) ; And I'd see A . For any character number, I could get an equivalent from the array, and the value would depend on exactly what I placed in the array beforehand. But storing the whole ASCII table is wasteful. In this contrived example, we don't want any characters less than 65 ('A') nor greater than 70 ('F'). So how can I make 85 equal 0? How about subtraction? Since characters are numbers, I could subtract 85 from each character I need to look up in the table. Then if the result is >= 0 and <= 5, I can just use the result of the subtraction directly as a subscript , like this: char charToLookup = 0 ; char result = 0 ; printf ( “Please enter the character to translate “ ) ; scanf ( “%c”, &charToLookup ) ; result = lookupTable[charToLookup – 85] ; printf ( “Your character %c, translates to %c”, charToLookup, result ) ; Of course if the user enters a character less than U or greater than Z, we'll likely get a run time error, but you can easily add if's to catch this. To further illustrate, the line that reads: result = lookupTable[charToLookup – 85] ; can actually be rewritten as: result = lookupTable[charToLookup – 'U'] ; that way you don't even have to Google to find out that U is equal to 85. Addition and subtraction work fine on characters. So to encipher a character in a very simple-minded way, just make an array with all the letters of the alphabet (or force everything to UPPER CASE, making your array half as big) and enter them in a scrambled, random order. Then as you enter characters, or read them from a file, just look up each one in the array and print the result. Presto, a Caesar cipher.
Image of page 4
Image of page 5
You've reached the end of this preview.
  • Spring '08
  • GeraldReed
  • Cryptography, Array, Enigma machine, rotor, Enigma

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern