0

I can't figure out how to wrap the k[i] in the formula c[i] = (p[i] + k[i]) % 26

I know the if and else code's are different, that's to illustrate the main implications I've tried. also the printf("%d", v); was intentional and used to try and find out where it was going wrong.

a key of aaa successful encrypts a message of similar length but if the message is longer than the key it will print the wrong encryption

any help would be much appreciated!

string d = argv[1]
printf("Encrypted message: ");
for (int i = 0, n = strlen(om); i < n; i++) /* checks if i th character of om is in the alphabet, if yes then checks if upper or lower and converts to alphabet value, adds key, and converts back to ascii value and prints. if not in alphabet, prints as it was */ 
{

    if (isalpha(om[i]))
    {
        if (isupper(om[i]))
        {
            int v = (int) d[i] % (strlen(d) - 1); 
            printf("%d", v);
            printf("%c",(((om[i] - 65) + v) % 26) + 65);
        }
        else
        {
            int y = d[i] % strlen(d);
            printf("%c",(((om[i] - 97) + y) % 26) + 97);
        }
    }
    else
    {
        printf("%c", om[i]);
    }
}   
printf("\n");
0

It's the indexer of the key that you have to wrap around, not the letter.

So if key == "abc"

you have

int v = (int) d[i] % (strlen(d) - 1);

so first i == 0, then i == 1, then i == 2 and when i >= 3, your d[i] will give a garbage value.

So the index of d should not get greater than the length of d.

Comment bellow if you need a more thorough explanation.

You should also check if he current char of the key is lowercase/uppercase and convert it to an int from 0 - 25 so that you can add it to the original message's characters.

Happy Coding! :)

2
  • 1
    ChrisG is right that the index should wrap, not the letter, but the explanation isn't very clear. As written, every letter will give a garbage value. The ascii value of any letter would be far larger than a strlen of 3, therefore, after the modulo operation is applied, the resulting value must be between 0 and the strlen of the key.
    – Cliff B
    Aug 23 '16 at 18:28
  • Sure. I just wanted to say that the modulo should be for the indexer only, and not at all for the letter. :)
    – ChrisG
    Aug 23 '16 at 19:09
0

I'm seriously despising this task, it's gone from something I thought would be quite simple to something that's taken me days and I still don't really get it

I have to be honest I don't really understand. I'm not too great with this yet so might need a simpler explanation. I understand the principle of what your saying, that d[i] < strlen(d) so would this be implemented by doing something like d[i % strlen(d)] ?

also in regard to the second part about the char of the key is lowercase or uppercase and convert it to an int would that be something like

for (int j = 0, n = strlen(key); j < n; j++)
{
    if (isalpha(key[j]))
    {
        if (isupper(key[j]))
        {
            key[j] = ((key[j] - 65) % 26)
        }
        else
        {
            key[j] = ((key[j] - 97) % 26)
        }
    }
else
    {
        key[j] = " "
    }

thanks again for the help

1
  • I think you are supposed to check the GetString() for isalpha() isupper() and islower(), not the key... Sep 19 '16 at 17:07

You must log in to answer this question.

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