Filter string array in C and count how many repeat

Asked

Viewed 37 times

0

The program makes car rentals, it takes license plate and the type of car, both stored in two different matrices, as I do to get only the license plates of the luxury type cars. Code I’ve made so far

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

int main()
{
char luxo[100]="luxo";

char type[100][100];
char typeCar[100][100];

char board[100][100];
char boardOpc[100][100];
char aux[100];

int quantity = 0, i = 0, j = 0, count = 0; //quantidade de locaçoes

int compar = 0, comparType = 0;

int client[100];

int payment[100];

int countA = 0, countB = 0, countC = 0, sair = 1;

while(sair==1){
    printf("Digite o tipo do veículo preferencialmente sem acento e letra minúscula: ");
    scanf("%s", type[countA]);
    printf("Digite a placa do veículo: ");
    scanf("%s", board[countA]);
    printf("Para continuar cadastrando mais uma locação digite 1, para ver as outras opções digite 0\n");
    scanf("%d", &sair);
    countA++;
}

sair = 1;
for(i;i<countA;i++) {
                for(j=i+1;j<countA;j++) {
                    if(compar=strcmp(board[i], board[j]) > 0) {
                        strcpy(aux,board[i]);
                        strcpy(board[i],board[j]);
                        strcpy(board[j],aux);
                    }
                }
            }
            
            count = 1;
            for(i=0;i<countA;i++){
                if(compar=strcmp(board[i],board[i+1]) == 0){
                    if(compar=strcmp(type[i], luxo)==0) {
                        printf("AQ");
                        count++;
                    }
                }
                else {
                    
                    for(i=0;i<countA;i++) {
                        for(j=i+1;j<countA; j++) {
                            if(compar = strcmp(board[i], board[j])>0) {
                                strcpy(aux,board[i]);
                                strcpy(board[i],board[j]);
                                strcpy(board[j],aux);
                            }
                        }
                    }
                    
                    for(i=0; i<countA; i++){
                        /*if(compar = strcmp(board[i], board[i+1])==0) {
                            if(comparType=strcmp(type[i], "luxo")==0) {
                                count++;
                            }
                        } else*/ if(compar = strcmp(type[i], luxo)==0) {
                            printf("Entrou AW");
                            printf("A placa %s aparece %d vezes\n", board[i], count);
                            
                            count=1;
                        }
                    }
                    if(compar=strcmp(type[i], luxo)==0){
                        printf("Entrou AQUI");
                        printf("A placa %s aparece %d vezes\n", board[i], count);
                    }
                }
            }
              }

1 answer

0

All its data is loose, that is, one vector has no relation to the other and this makes the program difficult to read and change. An alternative is to use structs to relate the data to each other. Example:

struct tipo_carro
{
    char tipo[100];
    char placa[100];
};
typedef struct tipo_carro Carro;

Now the type of car is totally related to your plate and this makes it easy to identify a type just using the plate and vice versa.

Carro carro[100];

Above I created a vector of type car where I can have 100 different cars, each of them with a type and a plate. To check all luxury type plates just do this:

for(int i = 0; i < 100; i++)
{
    if(strcmp(carro[i].tipo, luxo) == 0)
    {
        // Aqui será ativado quando encontrar um carro de luxo
        // Então basta fazer `carro[i].placa` para acessar a placa
    }
}

See these links about structs if you don’t know this kind of data: Structs - 1 Structs - 2

  • Blz thanks I’ll take a look

Browser other questions tagged

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