Blog‎ > ‎

Fun with C - II

posted 3 May 2012, 09:33 by olnf Admin   [ updated 17 Jun 2012, 10:59 by Sudeep Jaiswal ]
Author: Sudeep Jaiswal

What is the first thing that comes to your mind when I say the word ‘FLAMES’? Fire or a silly game you used to play in school? It was incredibly childish and completely nonsensical. Do you remember what it was, how it worked?

The method I know is this (There is at least one different method). We take two names and strike out common letters in pairs from both. Then we count the remaining number of letters in both names. This count is then used to cancel letters from the word ‘FLAMES’ until one letter is left. Now, each letter has a special meaning, which can sometimes result in absolute hilarity:

F – Friend
L – Love
A – Affair
M – Marriage
E – Enemies
S – Sister

I shall give an example to make things clearer. I’ll take the names Suparas and Gikesh and strike out common letters, like this:

S U P A R A S

G I K E S H


The second ‘S’ in Suparas will not be struck as there isn’t another ‘S’ in Gikesh. Remember, pairs of common letters. Now, counting the remaining letters, I get 11; 6 in the first name and 5 in the second. Now, using this count, I’ll strike out letters from the word ‘FLAMES’. The way I use is this. Start counting from ‘F’, reach ‘S’ and go back to ‘F’. Of course, it depends on the count. Now, suppose the 11th letter turns out to be ‘A’, it will be struck and the next count will start from the next letter, i.e.: ‘M’. Similarly, if ‘S’ was struck, counting will next start from ‘F’. This will continue until one letter is left.

F L A M E S

1 2 3 4 5 6


For this example, the flames count is eleven. Starting from ‘F’, the 6th letter is ‘S’, 7th is ‘F’, going on, we find the 11th letter is ‘E’, which gets struck. Now we are left with ‘FLAMS’, with count to start from ‘S’, the letter next to the one that was struck. Now, the 11th letter turns out to be ‘S’; it gets struck. Continuing this procedure leaves us with ‘M’. i.e.: marriage. So, according to the game, there will be wedding bells for Suparas and Gikesh in the future. And in reality, both are unmarried at the moment, so who knows?

---


When I thought of trying to make a C program that calculated this, I had a general idea of how I could do it. It eventually turned out to be too tedious and confusing, and I scrapped the original code I had written, but I digress. What I was absolutely sure about was that there were two things I was required to do. First, to cancel out common letters to get the flames count. And second, to use this count to cancel out letters from ‘FLAMES’.

Those of you who read ‘Fun with C’ will know about functions. If you haven’t, read it as it may help you in further understand the terminology I’ve used. Now, functions are of two types, library functions, which come bundled with C, and user defined functions. In this program, I created two such functions, one calculates the flames count and the second eliminates letters and leaves us the result. This program is over 100 lines long, so it’s not practical to show it here and explain it in detail, but I’ll run through it.

The way I’ve used to obtain the flames count is this. I compare the letters in the first name with those in the second. Firstly, the first letter of the first name is checked with the first letter of the second name, then the second letter, and so on. If a matching letter is found, it is deleted from the second name and checking now starts for the second letter of the first name. If all letters were checked and no match was found, then checking will start for the second letter of the first name. It too is checked with all letters in the second name, from first to last, depending on whether is match is found or not. This continues until all letters in the first name are checked. While this is done, I am also counting the number of letters struck. And since I know how long both names are, I subtract the number of letters that were struck from their sum to obtain the flames count:

Flames count = Length of first name + Length of second name – Letters struck


Now, to find the result, I have kept a predefined string with the word ‘FLAMES’ in it. On this string, the operation for striking out letters will be done. Now, counting manually is simple, but in a program? What I worked out was that no matter what the flames count is, that value subtracted with 6 will give us the position of the letter to be struck. Confused? I’ll give an example. I’ll take the one I took earlier. We had the flames count 11. So, doing it manually, we know that ‘E’ will go first. Now, according to my logic:

Flames count - 6 = Position of letter to be struck

i.e.: 11 – 6 = 5th letter


The 5th letter in ‘FLAMES’ is ‘E’, so the logic is correct. Now if the flames count is larger, say 23, we would continue to subtract 6 from it until we reach a value less than or equal to 6, but greater than zero, obviously. Try it manually, if you’re not convinced. After doing this programmatically, I was faced with a new problem; how to make the computer count from the next letter, ‘S’ in this example. I solved it by creating a new string with the remaining letters in the order in which the counting is to be done, ‘SFLAM’ in this example. I can tell you that in a program, it’s very easy to count from start to finish rather than somewhere in the middle. And after the next cycle, when we are next left with four letters, ‘SFLAM’ would get overwritten with the remaining letters in the proper order. This procedure will be continued until one letter is left and we have the result.

---


Entering first name…



Entering second name…



…Voilá!!



So, papa and mummy are enemies and have nothing in common? You may check the program yourselves. I have included the code too, if that interests you. But before downloading, a warning, as advised by my legal people: Results may not reflect or influence reality.

Download: flames.zip (9.65 kb)

Comments