0
for (int i = 0, n = strlen(p); i < n; i++)
{  
    if (isalpha(p[i]))
    {
        if (isupper(p[i]))
        {
            char stringshifttext = ((((p[i] - 'A') + (k[i % strlen(k)] - 'A')) % 26) + 'A');
            printf("%c", stringshifttext);
        }

        else if (islower(p[i]))
        {
            char stringshifttext = ((((p[i] - 'a') + (k[i % strlen(k)] - 'a')) % 26) + 'a');
            printf("%c", stringshifttext);
        }
    }  

I'm having trouble getting my key to apply universally.
K = BBBBB
P = ZzZzz
Result = A[A[[

K = bbbbb
P = ZzZzz
Result = GaGaa

When the key is uppercase and the inputed text is uppercase, it works.
When the key is lowercase and the inputed text is lowercase, it works.

If the key and the inputed text do not match case, it doesn't work.

Do I need to change the key somehow? Or change how it's being applied?

1 Answer 1

0

Quite simply, yes. You have implemented two of four possible combinations - the two where they are the same case. Now, you need to implement the cases where they are different case. Of course, if you were to figure out a way to handle the key where the case doesn't matter, then it would make things simpler. I suspect that you'll find tolower() or toupper() of use.

If this answers your question, please click on the check to accept it.Let's keep up on forum housekeeping. ;-)

1
  • I was able to get it to work by adding int j to my for loop and incrementing by 1 (j++) at the bottom of my isupper and islower sections. Question: why couldn't I include j++ at the bottom of my is isalpha section? Why did it only work after including it after both the isupper and islower sections?
    – hagan10
    Nov 6, 2015 at 19:55

You must log in to answer this question.

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