Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
basically i want to create a structure in C in which i am going to take 50 inputs of
1.country Name
2.Player name
3.batting average


and the printing of these 50 inputs should be like that no player name should be similar in same country.
e.g

India
Nikhil
possible case.


Australia
Nikhil
possible case.


again
India 
Nikhil
not possible case.

What I have tried:

C
#include <stdio.h>
#include <stdlib.h>
struct cricket
{
    char country_name[50];
    char player_name[50];
    int batting_average[50];
};
int compare_string(char[],char[]);



int main()
{
    int i=0;
    struct cricket t;
    int flag;

        printf("enter the player name\n");
        gets(t.player_name);
        printf("Enter country name\n");
        gets(t.country_name);
        printf("enter the batting average\n");
        gets(t.batting_average);
        flag=compare_string(player_name,country_name);

    int compare_string(char[],char[])
    {

            while(player_name[c]==country_name[c])
           {
               if(player_name[c]=='\0'||country_name[c]=='\0')
               break;
               c++;

           }
    }
     if(player_name[c]=='\0'&&country_name[c]=='\0')
        return 0;
    else
        return -1;
Posted
Updated 9-Mar-17 3:34am
v2
Comments
CHill60 9-Mar-17 9:13am    
What is your actual problem? (Apart from the fact your code won't compile)

There is a C standard library function to compare strings: strcmp - C++ Reference[^].

But your actual code is far away from a solution to your assignment. You would need an array of your struct cricket and an integer variable that indicates how many array entries are containing data. Then iterate over the array items containing data and check if new country and user name are identical to the values from the array item.
 
Share this answer
 
Comments
nikhil arora 9-Mar-17 9:30am    
can you show me through some e.g that how should i proceed to solve this problem?
Jochen Arndt 9-Mar-17 9:43am    
See the below solution by CPallini.
I suggest you to profitably use the C library function strncmp[^]. Try for instance
C
#include <stdio.h>
#include <string.h>


struct cricket
{
    char country_name[50];
    char player_name[50];
    int batting_average[50];
};

// returns 0 if the names match
int comp_cricket_names(struct cricket * pc1, struct cricket * pc2)
{
  int c = strncmp( pc1->country_name, pc2->country_name, sizeof(pc1->country_name));
  if ( c == 0 )
   c = strncmp( pc1->player_name, pc2->player_name, sizeof(pc1->player_name));

  return c;
}

int main()
{
  struct cricket ck[3];

  strcpy(ck[0].country_name, "India");
  strcpy(ck[0].player_name, "Nikhil");

  strcpy(ck[1].country_name, "Australia");
  strcpy(ck[1].player_name, "Nikhil");

  strcpy(ck[2].country_name, "India");
  strcpy(ck[2].player_name, "Nikhil");


  int i;
  for (i=0; i<2; ++i)
  {
    int j;
    for (j=i+1; j<3; ++j)
    {
      if ( comp_cricket_names(&ck[i], &ck[j]) )
        printf("team %d and %d have different names\n", i, j);
      else
        printf("team %d and %d have same names\n", i, j);
    }
  }
  return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900