Topical Information

This lab will help you practice with Cstrings and libraries.

Program Information

Often when using a text interface (console screen, printout, etc.), it is necessary to print a value that is too wide for the area allotted to it. For instance, if you look at many web printouts, you'll find that the URL has been abbreviated. If you went to print out the scores class example, the URL for the C++ file is:

(The '%20' parts are spaces in the original directory name. 20 is the hexadecimal value of the ASCII code for a space character.)

That's pretty wide — 97 characters to be precise. But what if the layout of the printed document allowed only 50 spaces to print the page URL? That could be a problem.

That's where you come in. The most common form of dealing with this is to remove some characters from the middle of the URL to make the beginning and ending fit within the limited space and hope this information is significant enough to guide the user back to the site should they need to return. So, the above URL might be compressed into:


Not great, but it was just a 'hope'/guide.

You get to write this routine. It should accept the original string and a width limit. It will produce a new string (do not destroy the original!) which has its middle part replaced with .... for an even width or ... for an odd width. ...what? Well, if the above example had called for a URL width of 45 instead of 50, the resulting string would have been:

By using a different length ellipsis for odd vs. even width's, you ease the calculation of what needs to be kept. For the width of 50 and a 4-char ellipsis, we get to keep 46 of the original characters: 23 from each end. For the width of 45 and a 3-char ellipsis, we get to keep 42 of the original characters: 21 from each end. So it is:

    if (odd(width)) // 3-char ellipsis
        keep = width-3;
    else            // even:  4-char ellipsis
        keep = width-4;
    keep_per_end = keep/2;

Place your ellipsis-izing function in a library.

Write a looped driver to test for proper ellipsis-izing.

Thought Provoking Questions

  1. There isn't an odd() function in the standard library. (Or an even() one for that matter...) How do you test for such a thing? Would this be in the same library with your ellipsis function? Is it a complicated function or a quick/simple one? What is the return type? What is the argument type?

  2. You think it's a good idea to print a ruler over the ellipsis-ized string to check yourself more easily. This comes to mind:

                 1         2         3         4         5         6

    How can you get the first line to print all spaced out like that? It lines up so neatly with the line below it...

  3. You can obviously count keep_per_end characters from the beginning of the string fairly easily. But how do you determine where keep_per_end characters from the end starts? Do you even need to know where it starts?

This assignment is (Level 2).