Below is my code for the given problem statement.
#include "stdio.h"
#include "string.h"
#define MAX_NAME_LEN 80
#define INVALID 0
#define VALID 1
#define ASCII_UPPER_CASE_LOWER_LIMIT 65
#define ASCII_UPPER_CASE_UPPER_LIMIT 90
#define ASCII_LOWER_CASE_LOWER_LIMIT 97
#define ASCII_LOWER_CASE_UPPER_LIMIT 122
#define UPPER_CASE_LOWER_CASE_DIFF ASCII_LOWER_CASE_LOWER_LIMIT - ASCII_UPPER_CASE_LOWER_LIMIT
typedef enum {EMPTY = 0, FRIENDS, LOVE, AFFECTION, MARRIAGE, ENEMIES, SIBLINGS} flames_t;
/* Validates if name contains only alphabets */
int validate_name(char* name, int len) {
int i = 0;
for(i = 0; i < len; i++) {
if(!(((name[i] >= ASCII_LOWER_CASE_LOWER_LIMIT)
&& (name[i] <= ASCII_LOWER_CASE_UPPER_LIMIT))
|| ((name[i] >= ASCII_UPPER_CASE_LOWER_LIMIT)
&& (name[i] <= ASCII_UPPER_CASE_UPPER_LIMIT)))) {
return INVALID;
}
}
return VALID;
}
/* Convert all characters to upper case */
void convert_to_upper_case(char *name, int len) {
int i = 0;
for(i=0; i < len; i++) {
if((name[i] >= ASCII_LOWER_CASE_LOWER_LIMIT)
&& (name[i] <= ASCII_LOWER_CASE_UPPER_LIMIT)) {
name[i] -= UPPER_CASE_LOWER_CASE_DIFF;
}
}
}
/* Eliminates common characters from both name by replacing them by '/0' */
void eliminate_common_characters(char *name1, int len1, char *name2, int len2) {
int i = 0, j = 0;
for(i = 0; i < len1; i++) {
for(j = 0; j < len2; j++) {
if(name1[i] == name2[j]) {
name1[i] = name2[j] ='\0';
break;
}
}
}
}
/* Count the number of characters in a string that has non-zero value */
int char_count(char *name, int len) {
int count = 0;
int i = 0;
for(i = 0; i < len; i++) {
if(name[i] !='\0') {
count++;
}
}
return count;
}
/* Determine applicable flames based on number of uncommon characters */
flames_t determine_applicable_flames(int count) {
//declaring array containing each item of flames
//all except 1 element will be one by one set to EMPTY based on calculation
flames_t flames[6] = {FRIENDS, LOVE, AFFECTION, MARRIAGE, ENEMIES, SIBLINGS};
//declaring variable to hold the result of calculation
//initializing by EMPTY
//if variable does not get reset, EMPTY will specify error in calculation
flames_t result = EMPTY;
//declaring a varibale to keep track of last deleted flames item
int index = 0;
int i = 0, j = 0;
//checking number of uncommon characters
//for 0 uncommon characters, flames should be Friends
if(count != 0) {
//five of six items of flames need to be removed
//removing one item per loop
for(i = 1; i <= 5; i++) {
j = 0;
while(1) {
//considering flames items that still exist
if(flames[index] != EMPTY) {
j++;
}
//if the current item is "uncommon character count" item
//remove the item by setting value to EMPTY
if(j == count) {
flames[index] = EMPTY;
break;
} else {
index++;
//flames items have index from 0 to 5
//reseting index to 0, when it's incremented after checking last item
index = index % 6;
}
}
}
for(i = 0; i <= 5; i++) {
if(flames[i] != EMPTY) {
result = flames[i];
break;
}
}
} else {
//setting flames to Friends
result = FRIENDS;
}
return result;
}
/* Display the applicable flames */
void display_applicable_flames(flames_t flames) {
switch(flames) {
case EMPTY:
printf("Error in calculation\n");
break;
case FRIENDS:
printf("Friends\n");
break;
case LOVE:
printf("Love\n");
break;
case AFFECTION:
printf("Affection\n");
break;
case MARRIAGE:
printf("Marriage\n");
break;
case ENEMIES:
printf("Enemies\n");
break;
case SIBLINGS:
printf("Siblings\n");
break;
}
}
int main() {
flames_t result = EMPTY;
char name1[MAX_NAME_LEN] = {0};
char name2[MAX_NAME_LEN] = {0};
int len1 = 0, len2 = 0;
int count = 0;
int i = 0, j = 0;
//taking names as input
scanf("%s", name1);
scanf("%s", name2);
//determining length of names
len1 = strlen(name1);
len2 = strlen(name2);
//validating names
if((validate_name(name1, len1) == INVALID)
|| (validate_name(name2, len2) == INVALID)) {
printf("Invalid names\n");
return 0;
}
//converting all characters of string to upper case
convert_to_upper_case(name1, len1);
convert_to_upper_case(name2, len2);
//eliminating common characters from both names
eliminate_common_characters(name1, len1, name2, len2);
//counting number of uncommon characters in both names
count = char_count(name1, len1) + char_count(name2, len2);
//determining the flames applicable based on the number of uncommon characters
result = determine_applicable_flames(count);
//displaying the applicable flame count
display_applicable_flames(result);
return 0;
}
Compile the file with above code and run as:
a.exe < input.txt > output.txt [on windows]
a.out < input.txt > output.txt [on linux]
Here,
a.exe or a.out are the executables generated after compilation
input.txt is the file containing two names for which FLAMES need to be calculated, e.g., if the names are Pradeep and Prad, then input.txt will be
Pradeep
Prad
P.S.:
1. The input can be in same line separated by space as
Pradeep Prad
2. The code considers that the maximum length of a name will be 79 characters. To change the maximum length modify the pre-processor directive MAX_NAME_LEN and recompile the code.
output.txt is the output file containing result, for given example output.txt will contain
Friends
-Pradeep