0

I've been trying to get my vigenere straight for some time now, managed to get rid of most of the bugs, but all I can see is that it still rotates the code for me even when I have a non-alpha characters. Could you please help me? Probably I declared j inside or outside of some loop i shouldn't have, but honestly, I have no idea. Any hint, help is highly appreciated. So this is what I managed to work out so far:

    string text = GetString();
    string keyword = argv[1];
    for (int j = 0; j < strlen(keyword); j++)
        {
            keyword[j] = toupper(keyword[j]);
        }
        // i want the key to code with lowercase only. does that work?

    for (int i = 0; i < strlen(text); i++)

    {
        if (isalpha (text[i])) 
        // for letters
        {   
            int j = i % strlen(keyword);
            if (isupper(text[i]))
            {
                int result = ((text[i] + keyword[j] - 2*'A') % 26) + 'A';
                printf("%c", result);

            }
            else 
            // for lower
            {
                int result = (text[i] + keyword[j] -'A' - 'a') % 26 + 'a';
                printf("%c", result);

            }
        }   
        else   

        {
            printf ("%c", text[i]);
           // should not proceed with the code letters but it still does 
        }
    }
        printf("\n");
// probably it shouldnt but prettier
        return 0;
1

You're on the right track that something is wrong with J. The problem is that you've tied the index for the key, j, to the index for text, i. int j = i % strlen(keyword); You are probably seeing the encryption fail when you process a non-alpha in text.

When a non-alpha is processed, such as a space, number or punctuation, the index for j must not be changed. J must be set independently and should only be incremented when a letter is encoded and should reset to 0 each time the last letter in the key is used.

If this answers your question, please click on the check to accept and remove your question from the unanswered question pool. Let's keep up on forum maintenance. ;-)

1
  • Thank you so much, I think that should work now! First thing tomorrow, I'll check in my appliance if I can fix it. :-) – nattakiri Aug 10 '15 at 18:43

You must log in to answer this question.

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