The Reversing Filter

Difficulty: ★ ★ ★ ☆

Filters are fun to code and useful, such as the more filter in Linux that pages text output. For this month’s Exercise, your task is to write a text reversing filter. This program is a bit more challenging than coding other types of filters.

As a review, a filter takes standard input, modifies it somehow, then generates standard output. This approach allows for text to be read directly from the terminal, input from a file, or redirected as output from another program. It’s this flexibility that makes filters so useful.

A common filter example is the Caesarian Cypher, which I wrote about a dozen years ago. (It was the August 2013 Exercise post.) This filter takes text and rotates the letters 13 positions to encrypt. Re-running the encrypted text through the same filter decrypts, which makes it useful but not a completely foolproof encryption method.

Most filters I’ve written about in this blog are simple I/O filters. But the task at hand is to reverse all input and generate output. For example, the following text is input:

This is a test

The filter generates this output:

tset a si sihT

The rub here is that the input text must be stored so that it can be processed in reverse order. This hurdle is what elevates your task beyond just writing a simple I/O filter.

No, you can’t cheat by having the filter output a “buffer full” message or something equally snarky. The filter must handle all input, whatever size, then regurgitate the input in reverse order for the output.

Click here to view my solution.

Leave a Reply