C Program To Play Flames Game | CodeChef | HackerRank

C Program To Play Flames Game | CodeChef | HackerRank

Flames Game

Flames Game is a relationship calculating algorithm famous between the youngsters. At the time of graduation everyone might heard about this and many of them tried out this secretly. Some took this as very serious also. So what FLAMES stance for?
F – Friends
L – Lovers
A – Affection
M – Marriage
E – Enemies
S – Sibling

How we can calculate the FLAMES?

It is very easy to explain with some example:
Your name: asd
Partner name: abcd

Mainly two steps are there:

  1. Get the flames count
    • Take the two names (‘asd’ and ‘abcd’)
    • Remove the common characters (two common characters ‘a’, ‘d’)
    • Get the count of the characters that are left (Removed a,d and the rest are s,b,c. So total 3.)
  2. Get the flames result
    • We take FLAMES letters (‘F’, ‘L’, ‘A’, ‘M’, ‘E’, ‘S’)
    • And start removing letters using the flames count we got.
    • And the letter which last the process is the result.

In our example we got flames count = 3. So first we takes FLAMES.
FLAMES
Then we start count from left up to flames count 3. Then remove the letter which is in the position 3. In this case it is ‘A’. So the letters become:
FLMES
Then we start count again from the letter which is removed ie, from ‘M’. So the next character to remove is ‘S’. So our letters become:
FLME
After next step:
FLE
Then:
FE
Last:
F
So the result is ‘Friend‘.

Input Format:

Input contains two lines

First line represents name 1

Second line represents name 2

Note: Input name should be case sensitive and name may consist of White spaces.White spaces are not taken to calculate Flames count.

(Eg. “bru ce  way ne”. Length of that name is taken as( No.of Characters – No.of White spaces ) 10.)if the two names equal ( Flames count is 0 ).So we have to print AFFECTION.

Output Format:

A single word representing Friends/Lovers/Affection /Marriage /Enemies /Sibling.

Sample Output

Algorithm

  • Get the Names from user as per the input format.
  • Pass the two Names into flamesCount()  function.
  • flamesCount() function calculate the length of the names after eliminating the common characters.It returns flames count.
  • Flames count is passed into the Flames() function.This function returns the last remaining character in the fl[]=”flames” .
  • Using switch case we have to print the relationship.

Programs

#include<stdio.h>
#include<string.h>
char* Flames( char fl[],int n,int fcount)
{
        int j=0,i,k;
        char buff[10]="";         //Initializing Buffer to 0
        if(strlen(fl)==1){
            return fl;          //Last Remaining character in fl[] returned 
        }
        else
        {
              for(i=0;i<fcount;i++)       //Traverse through fl[]="flames"
            {
               if(j==n)
                    j=0;      // if j reaches last character then j initialize to start 0 
               j++;
            }
            fl[j-1]='\0';       //Put NULL to Cancel the character in fl[]
            k=0;

            /*make copy of remaining character in fl[] char array.
             Eg. if fl[]="fl\0mes" it will store in buff as start with j to n-1 times 
             buff[]="mesfl\0"*/

            for(i=j;k<n;i++,k++){     
                if(i==n)               
                    i=0;                
                buff[k]=fl[i];
            }
            Flames(buff,strlen(buff),fcount); //Do this process recursively until fl[] becomes single character
        }
}
int flamesCount(char* first,char* second)
{
    int i,j;
    int len=strlen(first)+strlen(second);
    for(i=0;i<strlen(second);i++)
        if(second[i]==32)      //Neglecting White spaces of Partner name
            len--;
    for(i=0;i<strlen(first);i++) {
        if(first[i]!=32) { 
            for(j=0;j<strlen(second);j++)
                    if(second[j]!=32)
                        if(first[i]==second[j]) {
                            len=len-2;       //If Two Characters are same then minus 2 with overall length
                            first[i]=32;
                            second[j]=32;break;
                        }
        }
        else
            len--;       //Neglecting White spaces of Your name
    }
    return len;
}
int main()
{
    char first[20];
    char second[20];
    char fl[]="flames";
    char* ans;
    printf("Your Name : ");
    scanf("%[^\n]%*c",first);
    printf("Partner Name : ");
    scanf("%[^\n]%*c",second);
    int Fcount=flamesCount(first,second);
    if(Fcount==0){    
        printf("AFFECTION\n");  //Two Names are SAME then Print Affection in Default
        return 0;}
    ans=Flames(fl,6,Fcount);
    switch(ans[0])
    {
       case 'f' : printf("FRIENDS\n");break;
       case 'l' : printf("LOVERS\n");break;
       case 'a' : printf("AFFECTION\n");break;
       case 'm' : printf("MARRIAGE\n");break;
       case 'e' : printf("ENEMIES\n");break;
       case 's' : printf("SIBLINGS\n");break;
       default  : printf("Error");break;
    }
    return 0;
}

You May Also Like…

C Program To Find The Largest Word Of String | Coderbyte

Simple Minesweeper Game | C++

Find Straight City | Goldman Sachs

Hari Prasath R

You know who I am :)