Pages

Thursday, July 7, 2011

Peter's Address Stamp (or Strings as Arrays of Characters): Part 1 of Peter and the Postal Service

String are sequences of characters. In fact, in many programming languages, strings are implemented as an array of characters. Thus, in many languages you can access each character as you would any value in an array. For example, in C you might write my_string[0] to access the first character and my_string[9] to access the tenth.


Peter's address was composed of exactly three lines and 37 characters:

Peter Fencer

100 Library Way

Alexandria

The first line had exactly 12 characters, the second line had 15 characters, and last line had exactly 10. Peter knew this for a fact, because he had spent the entire morning trying to order an address stamp.


The form was exactly what you would expect a form for an address stamp to look like. It had three different lines for the three different lines of his address. Each line had a neat label, such as "Name" or "City", followed by 15 blank squares. To complete the form, all you needed to do was fill in the squares with the correct letters of your address. It was really quite simple.



But, Peter had now spent three hours staring at the form.


The problem was that the Imperial Stamp and Scale Company charged by the character. Peter wanted to guarantee that he was writing his address in the fewest letters while still maintaining correctness. After the first hour, he had convinced himself that there was no way to safely shorten his name or city. The address was more of a gray zone. He debated shortening the "Way" to a "W", but ultimately decided against it. Avoiding confusion was worth 2 letters.


The other thing that bothered Peter was blank space at the end of the line. Would he be charged for them? The address strings had to allow for spaces as valid characters. For example, the sixth character in the name string was a space that separated his first and last name. He knew how to handle such cases on library forms: depending on the type of form, you either used a special character to terminate the string (a NULL character) or also wrote down the length of the string. It was easy to tell where the string ended. Although Peter logically knew that the company should simply drop the trailing spaces, he was irrationally worried that they would not.


Finally, after spending the morning fretting over the form, Peter mailed it. In 4 to 6 weeks, he would have a brand new return address stamp. He sighed with relief.


He deeply hoped that this stamp would help clear up some of the problems that he was having with his mail. He had recently discovered that his handwriting was so terrible that no one in the post office could read the addresses. His last letter, which was supposed to go to his neighbor, was routed to West Atlantis! Even worse, the same problem applied to the return addresses, which meant that his letters could not even be returned. So, at least this stamp would solve that problem.


What Peter did not realize is that this address stamp was simply the beginning of a long and epic debate over the how strings should be used in delivering the mail.


-----------------


See more of Peter and the Postal Service in Part 2: Incorrectly Delivered Mail and Comparing Strings.


For more more discussion about arrays, see how Zed used arrays in his coffee shop menus.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.