### String Manipulation Problem

Question:

After dinner at a Chinese restaurant, Batman and Robin had a fortune cookie each. Both cookies contained the same fortune written on a single line on a strip of paper. After reading their fortunes, they both tore the strip of paper into 2 pieces and left promptly. You manage to find two pieces of torn paper, one containing the left half of the sentence, the other the right half. Unfortunately, they may not from the same piece of paper!From these two pieces, can you puzzle out the content of the original fortune? If it cannot
be determined, then print “The future remains a mystery”.

Sample Input:

er will coat every surface of the batmobile in silly putty.
On 6 September 2011, The Joker will coat eve

Output:

On 6 September 2011, The Joker will coat every surface of the batmobile with silly putty.

----------------------

Any suggestions on how to go about doing it?
I would work out how to do it myself, and then put that into programmatical terms.

I start off by putting one piece above the other. I compare letters vertically. If the first pair are the same, I move on to the next pair and compare them, and so on. Whenever I find that letters are not the same, I slide the top piece of paper one letter to the right and start again. When my comparisons reach the end of one piece of paper, they are exactly overlapping. If they never exactly overlap, I put the bottom piece above the top piece and try again until they are exactly overlapping.

I now create a new string by taking each letter in turn; if the letters are in the overlapping area, either letter. If not, whichever letter is there.

This translates easily into code, using two strings and a simple algorithm for comparing the letters.

This is programming. All that other junk about memorising syntax is just learning how to read and write. Programming is thinking about problems in a way that lends the solution to expression in the chosen programming language, and the implementation thereof.
Last edited on
thanks alot man. thats a really smart way of doing it.
Topic archived. No new replies allowed.