How do I compare two strings in c

Asked

Viewed 2,034 times

0

Basically what I want to do. It is receiving a String at the command line.Only the String entered, have to be in the binary base, so I do an if to check whether it is binary or not. My problem is in if , because regardless of the value you enter. Always enter if. - Thank you for your help!

int main(){

     char binario [50][100]={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};

     char nome [50][100];

     int contador=0;

      printf("Insira um valor");
      scanf("%s",nome[0]);

    while(nome[contador]!='\0'){



            if (nome[0] != binario[contador])
            {
                printf("Base inicial invalida");
                break;
            }

            contador++;
     } 

     return 0;

}
  • I reversed your edit because you changed the question problem, invalidating the answers given. If you have new questions, search the site to see if you find a similar problem, if not, ask a new question with the updated code, describing the exact problem (but be sure to have solved this step here before). I suggest a read on [Ask] for some guidelines that may help in formulating a new question.

2 answers

3


Note this expression:

if (nome[0] != binario[contador])

The result of this will be the comparison between the address of two memory pointers. These two addresses will never be the same, and therefore, he will always enter the if.

What you will want is to use the function strcmp:

if (strcmp(nome[0], binario[contador]) != 0)

In the meantime, I would like to point out that your programme has other problems which raise the following doubts:

  • Why declare 50 names if you only need 1?

  • Why convert from decimal to binary using strings, tabulated values and trial-and-error instead of calculating successive divisions for this?

  • I’m a beginner in C, had copied the binary String vector, then changed the names and forgot to change the values.

  • ** I did the way they suggested but still there is entering in the if **

2

You can use the function strcmp, declared in header file (or file header) string.h. Note that the function strcmp compares the content of strings and not your size.

Example:

strcmp(string1,string2);

The function will return you up to three values. They are:

  • = 0: both strings are identical.
  • > 0: the first different character has a higher value than the other string.
  • < 0: the first different character has a lower value than the other string.

See the example:

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

int main ()
{
  char key[] = "maçã";
  char buffer[80];
  do {
     printf ("Adivinhe minha fruta favorita? ");
     fflush (stdout);
     scanf ("%79s",buffer);
  } while (strcmp (key,buffer) != 0);
  puts ("Resposta correta!");
  return 0;
}

Exit:

Adivinhe minha fruta favorita? laranja
Adivinhe minha fruta favorita? maçã
Resposta correta!

Read here and here too in English.

  • 2

    The "strcmp" function is declared in the header file (or header file) "string. h". This file is not a "library", it is simply a source file containing statements. And in C and C++ "statement" and "definition" are technical terms, with different meanings.

  • Oops! Thanks, by the way we are learning even responding. And sorry for the mistake, I already edited the answer.

  • I also leave this link to supplement your comment.

Browser other questions tagged

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