How do I not repeat repeated array string

Asked

Viewed 54 times

-2

I need to make a query in a struct and compare all the .Location to print only 1 time without repetition, each one with their respective stored values.

struct archive{
    char    Title[N];
    char    autorName[N]; 
    char    pCompany[N];
    char    location[N];
    int     amount;
    int     releaseDate; 
    int     edition; 
    int     isbn;
};

My attempts to carry out this action have failed, here is an example:

            // listar cada local separadamente
                for(int i=0; i < quant+countBook; i++){
                    if(strcmp(book[i].location, book[i].location) == 0){
                        printf("%s\n", book[i].location);
                    }
                }
  • If although there may be repetitions you want to print only one then to find the first one. Now this (strcmp(book[i].location, book[i].location) == 0) will always be true since I can’t imagine how anything can be different from himself.

  • I agree, but it was the first thing that came to mind, although I tried to use another loop for then changing the second argument from strcmp() to j and likewise did not work. And the goal of this function is: to list the vectors of the struct in order by Location, and should not print twice or more the same Location since at first impression already show all the data related to it.

1 answer

0


Before printing check if it is a duplicate.

for(int i=0; i < quant+countBook; i++){
    int ja=0;
    for (int j=0; j<i; j++) {
        if(strcmp(book[i].location, book[j].location) == 0){
            ja++;
        }
    }
    if (ja == 0){
        printf("%s\n", book[i].location);
    }
}
  • Exactly what I need, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.