0

I have some problems with the vigenere assignment. No matter what I type in as plaintext, the output is always y. Example: argument = abc plaintext = alphA ciphertext = yyyyY

I used the debugger and found out that my encrypt function was always returning a value of 121 or 89 but I can't seem to fix this. Any help is greatly appreciated. Thanks

//program to encrypt user's input using vigenere cipher

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

int main(int argc, string argv[])
{
int encrypter(char c, char k);

//if user gives more than one argument
if (argc != 2)
{
    printf("Please only give one argument.\n");
    return 1;
}

//checks argument for nonalphabetical characters
for (int i = 0, j = strlen(argv[1]); i < j; i++)
{
    if (isalpha(argv[1][i]) == 0)
    {
        printf("Non alphabetical characters detected. Retry.\n");
        return 1;
    }
}


//gets the message from user
printf("plaintext: ");
string plaintext = get_string();


printf("ciphertext: ");
//encrypts the message from the user
for (int i = 0, j = 0, k = strlen(plaintext), l = strlen(argv[1]); i < k; i++, j++)
{
    if (isalpha(plaintext[i]) != 0)
    {
        int result = encrypter(plaintext[i], argv[1][j % l]);
        printf("%c", result);
    }

    else
    {
        printf("%c", plaintext[i]);
        j -= j;
    }
}

printf("\n");
return 0;
}

//function to encrypt the message
int encrypter(char c, char k)
{
   int cipher = 0;

   //changes argv[1] characters int0 numbers 0-25 for both upper and lowercase     letters
   if (k >= 'a' && k <= 'z')
   {
        k -= 'a';
   }

   else k -= 'A';

   //creates cipher text. Two conditions for lower and upper case plaintext
   if (c >= 'a' && c <= 'z')
   {
       cipher = (('c' + 'k') % 26) + 'a';
       return cipher;
   }

   else
   {
   cipher = (('c' + 'k') % 26) + 'A';
   return cipher;
   }
}
0

Stupid Mistake. Put a '' over my c and k thus turning it into fixed integer values.

| improve this answer | |

You must log in to answer this question.

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