Error comparing two C strings

Asked

Viewed 252 times

1

I type a value at the command line. That is in the binary vector. But if it is not return to the message from printf. But my problem is that it always goes into the cycle if, regardless of whether it is binary or not.

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

int main(){

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

 char nome [0][10];

 int contador=0;

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

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

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

        contador++;
 } 

 return 0;      
  • 3

    Are you sure this is the statement? The code does not produce what is in the statement nor closely.

  • 2

    It would be nice to [Edit] and add examples of what data you entered the test, what result you gave, and what results you expect.

2 answers

3


The code has several flaws and does not even closely what you are asking in the question. I will try to fix.

Note that the statements of arrays can be made to the size they are needed. This gives more efficiency. So binario only needs 16 elements and each of them only needs 5 bytes (4 characters + the terminator). nome just needs the array character, there is no other dimension. No dimension can be 0.

I preferred to use the for, although the while don’t be wrong.

The if is quite wrong. To know if n]ao found a text within the previously defined list you have to parse all items. The current code is analyzing the first item, if it is not equal it terminates the loop and terminates the search. That is, this code requires everyone to be the same to work. It’s not what you want. The solution is to let you search until the end, or until you find one of them. If you find one you have no reason to continue researching, just one to satisfy what you want.

But how do you know you didn’t? It’s simple, if he goes through all of them and never leaves the noose forcibly, he never did. To know that this occurred it is only to check if the counter went through all the elements and reached 16. Even if you find in the last element the break would force the output and the last increment would not be made, then the counter would be 15.

There are other problems in the code that I will not treat.

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

int main() {
    char binario[16][5] = {"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
    char nome[10];
    printf("Insira um valor\n");
    scanf("%s", nome);
    int contador = 0;
    for (; contador < 16 && strcmp(nome, binario[contador]) != 0; contador++);
    if (contador == 16) printf("Base inicial invalida"); //essa mensagem não faz sentido com o enunciado
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

1

According to his explanation, Cvoce must analyze the logic of its code to function according to the operating rules of each API. In the case of strcmp we have the following operating rule:

int strcmp(
   const char *string1,
   const char *string2 
);
Return Value
The return value for each of these functions indicates the ordinal relation of string1 to string2.
Value Relationship of string1 to string2
< 0   string1 is less than string2
  0   string1 is identical to string2
> 0   string1 is greater than string2

Therefore your code enters the IF because it is always returned non-zero value, since in no comparison locates an identical value.

Browser other questions tagged

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