0

I know that I've posted a question very recently about the same PSET, but I've encountered another problem and I've been trying to solve it for hours. Sorry if I'm wasting anyone's precious time.

My problem is that the plaintext keeps shifting by an unwanted letter of the key, the key's current letter index seems to be my problem, but I can't find it.

Thanks in advance for your help

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main (int argc, string argv[])
{
    // if command-line arguments are not 2
    if (argc != 2)
    {
        // print the instructions of the program
        printf ("Usage: ./caesar key , Ex: ./caeasar ABC (key must be alphabetical)\n");
        return 1;
    }
    int keyCheck;
    for (int k = 0; k < strlen(argv[1]); k++)
    {
        // keyCheck is equal to k'th char of the key
        keyCheck = argv[1][k];
        // if that char is not alphabetic
        if (!isalpha(keyCheck))
        {
            // print instructions
            printf ("Usage: ./caesar key , Ex: ./caeasar ABC (key must be alphabetical)\n");
            return 1; 
        }
    }

    string key = argv[1];

    printf ("plaintext: ");
    string plaintext = get_string();

    printf ("ciphertext: ");

    for (int i = 0, n = strlen(plaintext); i < n; i++)
    {
        int currChar = plaintext[i];
        int alphaOrder;
        int cipherVal;
        int asciiVal;
        // I declare the current key as the i'th letter of key mod string length of key
        int currKey = key[i % strlen(key)];


        if(isalpha(currChar))
        {
            currKey = (currKey + 1) % strlen(key);
            if(isupper(currChar))
            {


                alphaOrder = currChar - 65;
                // Vigenere's equation: char = current plaintext char + current key char
                cipherVal = alphaOrder + currKey;
                asciiVal = cipherVal + 65;
                printf ("%c", asciiVal);
            }
            else
            {
                alphaOrder = currChar - 65;
                // Vigenere's equation: char = current plaintext char + current key char
                cipherVal = alphaOrder + currKey;
                asciiVal = cipherVal + 65;
                printf ("%c", asciiVal);
            }
        }
        else
        {
            printf ("%c", currChar);
        }
    }
    printf ("\n");  
}
1

The index of the plain text and the index of the key must be independent of each other. When a non-alpha is processed, the plaintext key is incremented but the index of the key MUST NOT be incremented. In the code above, both are based on i. Does this look familiar? Create an int var to be used as the index for the key, let's say j. When a letter is encoded, j=(j+1)%strlen(key);

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

1
  • I very much appreciate your both responses, I've learned 2x more in one PSET than in the whole lecture. – Dominik Clemente Apr 27 '17 at 21:24

You must log in to answer this question.

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