How to compare a string of an array in C?

Asked

Viewed 3,593 times

1

I have the following variables :

char nome[10][8];
int i; --> Sendo esta para o loop

I ask the user to enter 5 names and then I read the variable :

printf("Entre 5 nomes : \n");
for(i=0; i< 5 ; i++)
{
scanf("%s",nome[i]);
}

But after that, I want to compare an array string and see if the name is correct, and if it is, it fires a message.

My attempt :

for (i=0; i < 1; i++) {
if (nome[i] == "Lucas de Oliveira");
printf("Idade : 18 anos, \nSexo : Masculino, \nEstado civil : Solteiro");
continue;
}

He appears the message, but he doesn’t even finish the first for to fill in all the names, and the check is somewhat incorrect.

Screenshot :

How can I proceed to make the verification correct, and if only the name that is entered that is inside the if, that the message appears.

  • 2

    You cannot compare strings directly in C. In this case, you are comparing the memory region between the constant and the content in nome. Study the function strcmp of string.h: http://www.cplusplus.com/reference/cstring/strcmp/

  • The command continue is being used in the wrong way. Whenever possible, let the loop follow its normal flow without interfering in its execution.

  • 1

    But the strcmp function works also for arrays ? Because when trying with it, the error.

  • How did you test it? And what you call a mistake? SIGSEGV?

1 answer

3


Problems:

  • Using the wrong function to read the string.
  • It is not comparing the string, but the address where it is.
  • Little space in the matrix.
  • Not iterating as often as necessary.

Starts by organizing the size of the matrix:

char nome[5][20];
int i;

In a matrix like this, what you have is 5 lines and 20 columns, and the name Marcos de Oliveira has at least 18 characters, which should be ideal.

puts("Entre 5 nomes :");
for(i=0; i< 5 ; i++)
{
   scanf("%19[^\n]%*c",nome[i]);
}

In this part you may not use the printf with \n, the puts is much simpler. Naturally the scanf stops reading when finding an empty space when using in conjunction with the %s, using that expression %19[^\n]%*c makes empty spaces used.

for(i=0; i < 5; i++) {
    if(!(strcmp(nome[i], "Lucas de Oliveira"))){
        printf("\nNome: %s\nIdade : 18 anos, \nSexo : Masculino, \nEstado civil : Solteiro", nome[i]);
        break;
    }
}

Here, just change the expression in the arguments of if using the function strcmp which compares two expressions, and returns 0 where they are identical, and also i < 1 for i < 5 which is the size of the matrix, otherwise it stops at the first iteration, and does not advance to the remaining names, and of course, remove the continue and put break in place, so iteration stops when finding the match.

Using fgets

As an alternative to the expression within the scanf, you could use the fgets:

for(i=0; i< 5 ; i++)
{
    fgets(nome[i], 26, stdin);
}

Like the fgets keeps breaking \n, should be removed in order to be able to compare with the desired word.

for(i=0; i < 5; i++) {
        if ((strlen(nome[i])>0) && (nome[i][strlen(nome) - 1] == '\n')){
            nome[i][strlen(nome) - 1] = '\0';
        }   
        if(!(strcmp(nome[i], "Lucas de Oliveira"))){
            printf("\nNome: %s\nIdade : 18 anos, \nSexo : Masculino, \nEstado civil : Solteiro", nome[i]);
            break;
        }
    }

If none or any of the functions used here are not working, check the documentation for alternative, or check the compiler settings in use.

  • 1

    Thank you very much for the answer, it worked out the way I had hoped. I’m new to C, and so I make a lot of mistakes, but the good thing is that I learn from them not to make them again.

  • 3

    @Mondial the ideal is to keep trying.

Browser other questions tagged

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