This lab will help you practice with Cstrings and libraries.
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:
http://craie-programming.org/122/samples/classes/scores%20(array%20member%20normal)/scores.C
(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:
http://craie-programmin....mber%20normal)/scores.C
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:
http://craie-programm...er%20normal)/scores.C
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.
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?
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 123456789012345678901234567890123456789012345678901234567890
How can you get the first line to print all spaced out like that? It lines up so neatly with the line below it...
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).
Add (Level 1) to perform the even/odd calculation without a ?: or if structure. Just a single math expression.
Add (Level 4) to allow the caller to specify where the dots should go. Commonly found possibilities are: ORIG...ORIG (like above), ORIG..., ...ORIG, ...ORIG.... But there are other (perhaps less sensical, but still valid) combinations, too: ...ORIG...ORIG, ...ORIG...ORIG..., and even ORIG...ORIG.... Basically you are letting the caller tell you if they want the ellipsis before the string, in the middle of the string, after the string, or some combination thereof.
Perhaps the easiest way to handle this is with a bit-mapped flag argument. But however you do it, your function must take a string, a width, and which dot positions are desired and produce a dotted string.
If they ask for multiple dot positions, try to leave as equally-sized chunks as possible around/between them.
In your driver, you might want to offer a menu with the dot position choices for testing.