my prof ask us to make a code for the game FLAMES using function oriented program.wherein the prog. will count the number of same letters to be cancelled in each name and will print the result.

ex.
julie ann :2
mjhay :2
marry

i dont know whats wrong w/ my code.here it is:

#include<stdio.h>
#include<string.h>
#include<ctype.h>


int cancelled();
int cancelled2();
int equiv(int cancelled,int cancelled2);

main()
  { 
char yn[30],hn[30];
int m,q,z,v,n;
clrscr();
printf("enter her name: ");
gets(yn);
	  printf("\nenter his name: ");
	  gets(hn);


		/*to unable upper cases*/
	  for(m=0;m<strlen(yn);m++)
	      {
	       if(isupper(yn[m]))
		  { yn[m]=tolower(yn[m]);

		  }


      for(q=0;q<strlen(yn);q++)
		{
		 if(isspace(yn[q]))
		 { yn[q]='.'; }
		}
	     }

	      for(z=0;z<strlen(hn);z++)
	      {
	       if(isupper(hn[z]))

		  { hn[z]=tolower(hn[z]);
		  }

		  for(v=0;v<strlen(yn);v++)
		{
		 if(ispunct(hn[v]))
		 { hn[v]=' '; }
		}

		}



      printf("\ncancelled in her name : %i\n\n",(int)cancelled);

      printf("canclled in his name : %i\n\n",(int)cancelled2);


	    n=equiv((int)cancelled,(int)cancelled2);

/*to print result*/

	      if(n==1||n==7||n==13||n==19||n==25)
	       {

	       printf("friends");
	       }

		else if(n==2||n==8||n==14||n==20||n==26)
	       {

	       printf("lovers");
	       }

		else if(n==3||n==9||n==15||n==21||n==27)
	       {

	       printf("angry");
	       }

		else if(n==4||n==10||n==16||n==22||n==28)
	       {

	       printf("marry");
	       }

		else if(n==5||n==11||n==17||n==23||n==29)
	       {

	       printf("enemy");
	       }

		else if(n==6||n==12||n==18||n==24||n==30)
	       {

	       printf("friends");
	       }





getch();

}

int cancelled()
  {     char yn[30],hn[30];
	int a,b,u;
	float ctr1=0.0;



	 for(a=0;a<strlen(yn);a++)
	   {
	       for(b=0;b<strlen(hn);b++)
		{
		   if(yn[a]==hn[b])
		     {
		       ctr1=ctr1+1;

		 for(u=0;u<strlen(hn);u++)
		   {
		     if(hn[b]==hn[u]&&u!=b)
		    {
		     ctr1=ctr1-0.5;
		     }
		   }
		 }
	   }
	}


return ctr1;

}


int cancelled2()
  {	char yn[30],hn[30];
	int c,d,u;
	float ctr2=0.0;

    for(c=0;c<strlen(hn);c++)
	     {

	       for(d=0;d<strlen(yn);d++)
		{
		   if(hn[c]==yn[d])
		     {
		       ctr2=ctr2+1;


		 for(u=0;u<strlen(hn);u++)
		   {
		    if(yn[d]==yn[u]&&u!=d)
		     {
		      ctr2=ctr2-0.5;
		     }
		   }
		  }
		 }
	    }



return ctr2;

}

int equiv(int cancelled,int cancelled2)
{  int n;
 n=cancelled+cancelled2;

 return n;
 }

this code will have a result of:

cancelled in her name:1015
cancelled in his name: 1162

wherein its wrong.please help me.:)

Dani AI

Generated

The function-oriented attempt in ’s first post has a few clear, practical problems that explain the weird outputs: the cancelled()/cancelled2() functions declare local char yn[30], hn[30] and never receive the strings from main (so they operate on uninitialized buffers); the code prints (int)cancelled (the function name) instead of calling cancelled(…); the return types and arithmetic mix float and int (the 0.5 trick is fragile); several loops use the wrong string length (indexing hn with a strlen(yn) limit) which can overflow; and unsafe/nonstandard calls like gets, clrscr and getch are used. As asked, the expected result for the example in Post #3 is “SWEETHEARTS” (that example can be used as a test case).

A reliable, function-oriented approach:

  • Preprocess both names: remove non-letters and lowercase.
  • Build frequency arrays int a[26], b[26] and count letters.
  • Compute common = sum(min(a[i], b[i])).
  • Remaining letters = len1 + len2 - 2*common.
  • Use the remaining count to run the FLAMES elimination (rotate and remove until one letter left).

Example helper functions (keeps logic in functions and avoids the earlier mistakes):

int count_common(const char *x, const char *y) {
  int ca[26]={0}, cb[26]={0}, i, common=0;
  for(i=0; x[i]; i++) if(isalpha((unsigned char)x[i])) ca[tolower((unsigned char)x[i])-'a']++;
  for(i=0; y[i]; i++) if(isalpha((unsigned char)y[i])) cb[tolower((unsigned char)y[i])-'a']++;
  for(i=0;i<26;i++) common += (ca[i] < cb[i]) ? ca[i] : cb[i];
  return common;
}

char flames_result(int remaining) {
  char f[] = "FLAMES"; int len=6, idx=0, j;
  while(len>1) { idx = (idx + remaining - 1) % len; for(j=idx;j<len-1;j++) f[j]=f[j+1]; len--; }
  return f[0];
}

Practical tips: pass strings into functions (do not redeclare/name-shadow buffers), return int for counts, call functions with parentheses and store their results before printing, avoid gets (use fgets and strip newline), compute strlen once if used repeatedly, and test with known examples (e.g., the “julie ann” / “jan paul” case) to verify the elimination logic.

Recommended Answers

All 3 Replies

I've never heard of "Flames". Since you've had no responses, I suspect others haven't either.

What should your output be, and what have you tried to fix it, so we don't have to repeat it?

Thanks for using code tags. ;)

oh i see . .im sorry for that. .in the game flames it stands for:

F=FRIENDS
L=LOVERS
A=ANGRY
M=MARRIED
E=ENEMY
S=SWEETHEARTS

the game goes like this.

you get a couples name example:

julie ann
jan paul

and then compare the first name and the second name ..count the number of the same letters.there are 6 same letters in the first name(j,u,l,a,n,n)
6 in the 2nd name(j,a,n,a,u,l).add the # of sme letters.
so there are 12.

the result is SWEETHEARTS.


.. im sorry for not good explanation . .:)

if it is not in program oriented the code goes like this . .

#include<stdio.h>
#include<string.h>
#include<ctype.h>
main()
	{
	  char yn[30],hn[30],x;
	  int a,b,c,d,n,u;
	  float ctrA=0.0,ctrB=0.0;
	  clrscr();

	  printf("enter her name: ");
	  gets(yn);
	  printf("\nenter his name: ");
	  gets(hn);
	  strlwr(yn);
	  strlwr(hn);


/*to count the number of same letters*/



for(a=0;a<strlen(yn);a++)
{
for(b=0;b<strlen(hn);b++)
{ if(isspace(yn[a]))
{ a++;}
if(yn[a]==hn[b])
{
ctrA=ctrA+1;
for(u=0;u<strlen(hn);u++)
{
if(hn[b]==hn[u]&&u!=b)
{
ctrA=ctrA-0.5;
}
}
}
}
}


printf("\n\ncancelled in her name : %i\n\n",(int)ctrA);

for(c=0;c<strlen(hn);c++)
{
for(d=0;d<strlen(yn);d++)
{ if(isspace(hn[c]))
{c++;}
if(hn[c]==yn[d])
{
ctrB=ctrB+1;
for(u=0;u<strlen(yn);u++)
{
if(yn[d]==yn[u]&&u!=d)
{
ctrB=ctrB-0.5;
}
}
}
}
}
printf("cancelled in his name : %i\n\n",(int)ctrB);


	     n=ctrA+ctrB;
/*to print result*/
if(n==7||n==13||n==19||n==25)
		{
printf("friends");
	       }

		else if(n==2||n==8||n==14||n==20||n==26)
	       {

	       printf("lovers");
	       }

		else if(n==3||n==9||n==15||n==21||n==27)
	       {

	       printf("angry");
	       }

		else if(n==4||n==10||n==16||n==22||n==28)
	       {

	       printf("marry");
	       }

		else if(n==5||n==11||n==17||n==23||n==29)
	       {

	       printf("enemy");
	       }

		else if(n==6||n==12||n==18||n==24||n==30)
	       {

	       printf("friends");
	       }


	getch();


	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.