Conditional does not work as expected (related to ASCII table)

Asked

Viewed 87 times

1

int tam, i;
char msg[1000];
printf("Escreva a mensagem para ser criptografada: ");
fgets(msg, sizeof(msg), stdin);

tam = strlen(msg);

for (i=0; i<tam; i++) {
    int aux;
    aux = msg[i];
    if ("A"<=msg[i]<="B" || "a"<=msg[i]<="z") { // O problema acontece aqui
        msg[i] = msg[i]+3;
    } else {
        msg[i]== aux;
    }
}

When I walk in with "Texto #3", the program should only change the characters between 65 and 90 or between 97 and 122. However, in the character space (32) and all others enter the first on parole.

The result comes out Wh{wr#&6, and the expected was Wh{wr #3.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

2 answers

2

I think you wanted to use single quotes (apostrophe) and not doubles. There is a big difference between them. single quotes indicate a single character and double quotes indicate a sequence of characters terminated by a null value. Although it has only one character, it is considered a valid string character and still has the terminator, and still needs to compare all characters in the string to indicate equality, but it doesn’t look like you want this, you just want to compare a character.

In addition this code neither compiles, at least in the presented form, then either presented a different one than what is using or is reporting a different error than what happens. There is an error in the construction of the Boolean expression within the if mixing operands inappropriately, you can not use as in mathematics, can only compare two operands at a time in the operators.

I’m not judging logic because the question doesn’t make it so clear, there may be error there too.

I’m going to simplify some things and do it the right way in C.

char msg[1000];
printf("Escreva a mensagem para ser criptografada: ");
fgets(msg, sizeof(msg), stdin);
for (int i = msg; *i != '\0'; i++) *i = ((*i >= 'A' && *i <= 'Z') || (*i >= 'a' && *i <= 'z')) ? *i + 3 : *i;

I put in the Github for future reference.

There’s actually an obvious logic error there. If 3 letters are missing to finish the alphabet it should start again there at the beginning, then the letters Y, W and Z should turn into A, B or C. I let you hit this until to continue the exercise.

Behold: Whenever I am going to scan a string in C I must use "strlen()", right?.

1

I don’t know if this covers the entire code, but I can tell you have two problems with your if

Before the comparison a <= b <= c does not work in C, as the compiler will first compare a with b, generating the result 0 (false) or 1 (true), and then comparing this 0 or 1 with c, what is not what you want. Right would be a <= b && b <= c

According to what you declare between double quotes is an array of characters (a literal string), while what you declare between single quotes is a character, when you compare "A" <= msg[i], you are using the pointer that points to the first character of the string "A" for comparison, not the character 'A'.

  • Thank you, those were the two mistakes.

Browser other questions tagged

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