Changing character values in C

Asked

Viewed 552 times

0

I’m trying to change the value of the characters by adding the current value plus some value provided by the user:

char texto[7] = "barfoo";
int tamanho = strlen(texto);

for(int i = 0; i < tamanho; i++)
{
    printf("%c\n", texto[i] + 23);
}

As in the example I have as input the text "barfoo" and the expected output would be "yxocll", however the output that is generating is as follows:

inserir a descrição da imagem aqui

The first two letters come out right, but the next ones don’t come out as expected.

I wonder if it can be related by the fact that the ASCII table goes up to 127. If it is would have some way to get around this problem.

  • Do you want only letters? or numbers and signs too?

  • Hello Anderson, just the letters, in the complete file I already made a function to skip the special characters and the numbers

  • So, why aren’t you skipping the special characters. The } is right

  • Thank you Anderson

2 answers

3


It has to do with the fact that the ASCII table goes up to 127 (not 125). When you exceed this value you start printing the values of the extended tables. In fact the problem seems to be even more restrictive than the ASCII table.

To circumvent this you have to define a criterion of what to do. Defined this criterion you must implement in the code. Programming is more about understanding the problem and finding a solution than writing code. So this is not about knowing or not knowing C but thinking about the solution of the problem. The output gives a hint of what to do, but it is not explicit. You have to exercise logic. So you can see when the character exceeds the limit of the letters, that is, 122 it starts to do -3 instead of more 23. So you can get paroled, something like that:

texto[i] + (texto[i] > 'c' ? -3 : 23)

I put in the Github for future reference.

This looks like the cypher of Caesar. See there a complete implementation and note that there is done in a more linear way. If it is, it is a play encryption, just to illustrate with stirring a data that is slightly overshadowed from its normal content. Don’t take this to mean cryptography which is an extremely complex subject.

I take this opportunity to say that you should not use strlen().

  • Thanks Maniero, I really felt the logic, but I could understand where I was wrong. Guy had been analyzing the code since morning, and hadn’t noticed this pattern.

1

This is because you are just "going forward" - your code has no prediction to go back to the letter 'a' after passing the 'z'.

For the C language, characters and numbers between -128 and +127 are indistinct - all are the type "char" - but the code of 'z' + 23 is greater than +127 - so you can use unsigned char if you want to check on a if only after the sum. Your code could look like this:

#define CHAVE 23

int main() {

    char texto[7] = "barfoo";
    unsigned char novo;
    int tamanho = strlen(texto);


    for(int i = 0; i < tamanho; i++) {
        novo = texto[i] + CHAVE;
        if (novo > 'z') {
             novo -= 26;
        } 
        printf("%c", novo);
    }
    printf("\n"); 
    exit(0);
}

This code works for keys up to 26: when finding any character code that is beyond the lowercase 'z', it subtracts 26 back to the beginning of the alphabet, and counting from there. It’s still far from ideal that would work for upper and lower case characters as well as other symbols.

Notice some other suggestions - for example, separating the value from chve to a point in the file where it is more readable, and easy to change.

As a final note - bear in mind that this type of experience is very cool for programming learning, and even for personal fun and friends - but as serious encryption it does not work. This type of encoding is known as the "Cezar cipher" - and for centuries it has been widely used - but with the formal advances of cryptography in the last century, it has only become a toy, and any professional in the field can discover the key in a few minutes (or less)after only striking eye at the ciphertext in this way.

  • Hello jsbeuno’s, thank you. I am just making these challenges to improve my logic.

  • Thanks for the reservations. I will analyze where I went wrong and understand where I can improve

  • Actually, it’s not technically "mistake" -it was really missing the part of going back to the beginning. The other suggestions are not even about mistakes - they are things that only with practice you get.

Browser other questions tagged

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