0

i have been trying so hard to figure out what the shifting problem is in the encrypt function but i have no clue so i would like it if any one could tell me what is happening. I have put a "----here----" marker before the line the problem is happening.

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

//an encrypter function
void encrypt(string plainText, string key);

//check if the key is valid
bool checkKey(string key);

//check if a charater is an int
bool isInt(char value);

int main(int argc, string argv[])
{
    // check if there aren't more than one 
    // command line arguments
    if (argc != 2)
    {
        printf("Usage: ./vigenere keyword\n");
        return 1;
    }

    //check if the key is valid
    if (!checkKey(argv[1]))
    {
        printf("Usage: ./vigenere keyword\n");
        return 1;
    }

    string plainText = get_string("plaintext: ");
    //intf("here");
    encrypt(plainText, argv[1]);

}

void encrypt(string plainText, string key)
{    
    printf("ciphertext: ");
    int keyIndex = 0;

    //iterate through the plaintext and encrypt each
    //character then print it to the screen
    for (int i = 0; i < strlen(plainText); i++) 
    {
        if (islower(plainText[i]))
        {       
            //----here----
            printf("%c", ((int)(plainText[i] + key[keyIndex] ) % (int)'z')); 
        }
        else if (isupper(plainText[i]))
        {
            //----here----
            printf("%c", ((int)(plainText[i] + key[keyIndex] ) % (int)'Z') ); 
        }
        else
        {
            printf("%c", plainText[i]);
        }

        if (keyIndex<strlen(key))
        {
            keyIndex++;
        }
        else
        {
            keyIndex = 0;
        }

    }
    printf("\n");
}

bool checkKey(string key)
{

    //iterate througth the key and check
    //if all of its digits are non number characters
    //else return flase
    for (int i = 0; i < strlen(key); i++)
    {  
        //if the digit is an int 
        //return false because we want a word not an int
        if (isInt(key[i]))
        {
            return false; 
        }
    }

    //return true at last if all the digits
    //are numbers
    return true;
}
bool isInt(char value)
{
    //if the character is between 0 and 9 then
    //it is an int 
    if (value >= '0' && value <= '9')
    {
        return true; 
    }
    else
    {   
        //if it isnt an int return false
        return false;
    }

}

1 Answer 1

1
    //else return flase

I love that kind of humour.

Have you done caesar first? vigenere is almost the same, just that you need to convert the right character of the key to a number first, turning 'a' and 'A' both to 0, 'b' and 'B' both to 1, and so on. caesar usually involves a %26, but a %'z' would probably be abusing % for when you really mean -.

Pick your working caesar solution, and change based on that one. I hope you weren't as creative with % there.

Also, the key index should be incremented only on encrypting a letter, not on printing a non-letter.

And instead of if (isInt(key[i])) (which could use isnum instead), maybe better use if (!isalpha(key[i])), in case there's more non-alphabetic than just digits.

2
  • thanks, i have done what you told me and it worked. i have also taken the comments, i will keep in my mind, though i didn't quit understand what tone you were using when saying "I love that kind of humour".
    – EHM
    Commented Mar 1, 2019 at 18:20
  • Writing the word "false" false :) (though it makes more sense in German, where "false" and "wrong" are same word)
    – Blauelf
    Commented Mar 2, 2019 at 15:59

You must log in to answer this question.

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