0

I can't wrap my mind around why some letters get properly shifted and others don't, when I have a k of greater than 26.

For example, one of the checks is p=barfoo, k=65 and I get ÏÎeÓbb instead of onesbb. So r and o get properly shifted but the rest go out of the table and into the extended ASCII codes.

Here is my code. Its not revised for efficiency and good design yet. Trying to get it to work first.

        if (p[i] >= 'a' && p[i] <= 'z')
        {
            if ((p[i] - 97) + k >= 26)
            {
                int shift = k % 26;
                printf ("%c", (p[i] + shift - 1) % 122 + 97);
            }
            else 
            {
                printf ("%c", p[i] + k);
            }
        } 

1 Answer 1

1

How I solved this:

After I declared

int key = atoi(argv[1]);

I placed an if condition:

if (key > 26){
    key = key % 26;
}

And that solved the issue.

1
  • Yeah I figured it out a different way but that works. May 12, 2017 at 18:52

You must log in to answer this question.

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