0

I was trying to do substitiution on two loops but i had some issues. i found onle loop solution here https://stackoverflow.com/questions/61767483/free-invalid-pointer-aborted-core-dumped-cs50

but now i dont really undestand this part : int x = plaintext[i] - 'a'; if plaintext[i] is 'm'. m is 109 in ascii and a is 97 so m-a =12 form feed in ascii. or its not an ascii substraction going on?

1 Answer 1

1

In effect it is not an ascii value for an alphabetical character, if, however, it is not what we are trying to do, but what we want is to substitute the m character for the corresponding one of the key.

For example if the key is key: JTREKYAVOGDXPSNCUIZLFBMWHQ, and we want to encrypt the character m, we must use the character 12 in the key, that is, if we write key as an array we will write key[12], X.

EDIT:

So, it is necessary to obtain from a character 'A', for example, its position in the alphabet, the first. But since we start counting from 0 (due to the indexing of the arrays) the usual thing is to do for a plain text character:

// lowercase
plaintext[i] - 97 or plaintext[i]% 97 or plaintext[i] - 'a'


// uppercase
plaintext[i] - 65 or plaintext[i] % 65 or plaintext[i] - 'A'

Of course we will do this in a for with the appropriate indexes

2
  • The thing is that I dont uderstand why there is "- a". As i understand the idea is to know what number in prlintex coresponds with the key. bu isnt it iteration of i itself is acutally that coresponding number? if i starts with 0 and that is also the first place in array of the key?
    – Adr
    Jun 1, 2020 at 14:42
  • You can read my edition. You are right, perhaps my explanation has not been clear, the question is that if we have a character, let 'b' with an ascii code equal to 98, the previous expressions allow us to reduce it to find the appropriate key index. You must make sure you understand the characteristics of the problem well.
    – MARS
    Jun 1, 2020 at 15:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .