Allow Me to Squeeze In Here…

When it comes to editing text, the concept of copy and paste is an old one: You select a chunk of text, choose its new location, then paste in the text. The surrounding text jiggles around to make room. Neat and tidy.

This month’s exercise involves programming that copy-and-paste operation. It involves working with strings and pointers, which are closely related so if you are confuzzled by one then you’ll find yourself cheerfully confuzzeld by the other.

The task is to take this string:

That's the biggest spider I've ever seen!

And insert the text , nastiest after the T in biggest so that the resulting string looks like this:

That's the biggest, nastiest spider I've ever seen!

(Text coloring on this page is for illustrative purposes.)

As with most programming puzzles, multiple solutions exist. This month I want you to come up with two of them.

Yes, I am cruel.

The first solution is the cheat: Don’t create the string, merely display it. Have the program output the text That's the biggest, nastiest spider I've ever seen! one chunk at a time. That’s the easy way to solve the problem.

The second solution is to create the string in memory, then display the whole thing.

Even with these two tasks, you’ll find many potential solutions. Some of them involve using pointers, some involve using the various strings.h functions, others involves insane solutions no one has yet thought of.

As usual, please attempt the exercises on your own before you look at my suggested solutions, linked to below.

Solution 1: Display the string

Solution 2: Build the string, then display it

Leave a Reply