March 1996 Programmer's Challenge
Words The Reverse
Mail solutions to:
progchallenge@mactech.com
Due Date: 10 March 1996
Text input, of Block given a in words of order the place (in
reverse) will that routine a write to is challenge the month this.
Oops, what I meant to say was: This month, the Challenge is to write
a routine that will reverse (in place) the order of words in a given
block of input text. The prototype for the code you should write is:
pascal void ReverseTheWords(
const char *text, /* the words you should reverse */
const long numCharsIn /* length of inputText in chars */
);
Specifically, ReverseTheWords should exchange the first word in
the input text with the last word, the second word with the
next-to-last word, etc. For the purpose of this Challenge, a word is
defined as a continuous sequence of alphanumeric characters
[a..zA..Z0..9]. Any nonalphanumeric characters should remain in their
original positions and in their original order with respect to the
new words; that is, punctuation, white space, and other characters
between the first and second input words should, on output, be
located between the new first and second words. As an example,
ReverseTheWords would convert:
This, however, <-is-> a (difficult) test.
into
Test, difficult, <-a-> is (however) this.
As you can see from the example, there is one additional
requirement. Your code needs to adjust the capitalization of words so
that the n-th word is capitalized on output only if the n-th word was
capitalized in the input text.
There are no specific restrictions on the amount of auxiliary
memory you may use (within reason), so you may allocate a few buffers
of size numCharsIn should you need them. Remember, however,
to deallocate any memory you allocate before returning, as I will be
calling your code many times.
Note that the prototype specifies the use of Pascal calling
conventions. That is because this month we are conducting ...
A Language Experiment
Over the past months, in response to suggestions from readers, we
have made a number of changes to the Challenge, including migrating
to PowerPC native code and expanding to other C compilers. Now we are
experimenting with some additional changes. This month, for the first
time, your solution to the Challenge can be coded in C, C++, or
Pascal, using your choice among the MPW, Metrowerks, or Symantec
compilers for these languages. Although either 68K or PowerPC code is
permitted, I will be running your code on a PowerMac 8500, so native
code is obviously recommended. The environment you choose must
support linking your solution with test code written in C. Along with
your solution, you should specify the intended environment, compiler
and compiler options, or (better yet) provide a project file or make
file that will generate a stand-alone application that calls your
solution from C test code.
Post-Publication Clarification
The published problem did not indicate how to capitalize the
output when the input contains a word that starts with a numeric
character. For example, "Catch 22." could be converted to "22 Catch."
or to "22 catch.". Because the published problem statement was silent
on how to deal with this case, either approach will be considered
correct.
If you have any questions, please send me e-mail at one of the
Programmer's Challenge
addresses, or directly to
boonstra@ultranet.com.
Back to the Programmer's Challenge Page
Last modified by Bob Boonstra on 3/21/96.
|