Help with ROT-13 C encryption

Asked

Viewed 243 times

2

Some letters of the encryption are passing from 'z', and going to symbols in the ASCII table, why is this occurring?

#include <stdio.h>  // CRIPTOGRAFIA ROT13 
#include <stdlib.h> // A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
#include <string.h>

int main()
{
    int i;
    char Palavra[255];

    printf ("Digite a palavra que deseja criptografar: ");
    scanf (" %s", Palavra);

    for (i = 0; i < strlen(Palavra); i++)
    {
        if (Palavra[i] >= 'a' && Palavra[i] <= 'z')
        {
            Palavra[i] = Palavra[i] + 13;
        }
            if (Palavra[i] > 'z')
            {
                Palavra[i] = Palavra[i] - 26;
            }
    }   

    printf ("%s\n", Palavra);

    return 0;
}
  • For which input did you realize this happened and what was the output generated?

  • By chance your input that presented error has uppercase letters?

  • @Max comparison between char exists and is considered the alphabetical order (actually it is compared the respective integer values, but the result is the same).

  • No, no uppercase letters... The input was: Gui // The output: t?v // https://imgur.com/vja1l5n @Andersoncarloswoss

  • The way you did when adding 13 you can get a number outside the standard ASCII table (from 0 to 127) and, depending on the character set used, for example. ISO-8859-1 or even UTF-8, may represent an invalid value for character representation.

  • 2

    The character 'u' is the value 117, b'01110101'; when you sum 13 you will get the value 130, b'10000010'. As you are doing mathematical operations, this value will be treated as an integer and, for the integer, the first bit represents the sign; that is, you do not get the value 130, but -126. This way when you check whether you passed from 'z' you are comparing if -126 is greater than 122; since it is not, the value -126 remains. It is not an ASCII character, so it is displayed the "?".

  • I’m pretty sure there’s some question/answer on the site treating this very thoroughly, so possibly it would be duplicate.

  • 1

    Your problem has to do with the type actually. Once you used char instead of unsigned char, when sum 13 in the 'u' that is 117 gets -126 and not 130, and so if you have then if (Palavra[i] > 'z') is not executed as the value is lower -126 < 122. Change the guy to unsigned char your code is working

Show 3 more comments

1 answer

2


Let’s make it simple. What you should do is you should add up to half of the alphabet 13, if you pass that part you should subtract 13. Isn’t it simpler? I took advantage and improved performance by eliminating a loop needlessly.

#include <stdio.h>

int main() {
    char palavra[255];
    printf("Digite a palavra que deseja criptografar: ");
    scanf(" %s", palavra);
    for (int i = 0; palavra[i] != '\0'; i++) {
        if (palavra[i] >= 'a' && palavra[i] < 'n') palavra[i] += 13;
        else if (palavra[i] >= 'n' && palavra[i] <= 'z') palavra[i] -= 13;
    }   
    printf("\n%s\n", palavra);
}

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

  • That’s right, it worked. ?

  • A little complicated because you did not detail the mistake

  • Look at the exit: Imgur.com/vja1l5n

Browser other questions tagged

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