Can't figure out what I am doing wrong here. I am guessing it has to do with the way I am incrementing through the keyword.
Sample below:
firecore@ide50:~/workspace/pset2 $ ./vigenere abcdefghijk <--run program w/ keyword
aaaaaaaaaaa <-----enter text to be incrypted
abcdtabatak <--- result. I am assuming this should encrypt to abcdefghijk*
check50 results:
firecore@ide50:~/workspace/pset2 $ check50 2015.fall.pset2.vigenere vigenere.c
:) vigenere.c exists
:) vigenere.c compiles
:) encrypts "a" as "a" using "a" as keyword
:) encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword
:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword \ expected output, but not "CaSGop\n"
:( encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword \ expected output, but not "CASGOP\n"
:) handles lack of argv[1]
:) handles argc > 2 :) rejects "Hax0r2" as keyword
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc,string argv[])
{
string sKeyWord = "";
int iKeyLength = strlen(sKeyWord);
int iKey[iKeyLength];
string sPlainText = "";
int iAccept = 0;
if(argc != 2)
{
printf("Invalid argument! Please enter program name and keyword.\n");
return 1;
}
else if(argv[1])
{
iKeyLength = strlen(argv[1]);
for(int i = 0; i < iKeyLength; i++)
{
if (!isalpha(argv[1][i]))
{
printf("Invalid entry, please use letters only.\n");
return 1;
}
else
{
iAccept = 1;
sKeyWord = argv[1];
}
}
}
for(int i = 0; i < iKeyLength; i++)
{
iKey[i] = toupper(sKeyWord[i]) - 65;
}
sPlainText = GetString();
int iPlainText = strlen(sPlainText);
for(int j = 0, k = 0; j < iPlainText; j++)
{
if(!isalpha(sPlainText[j]))
{
printf("%c",sPlainText[j]);
}
if(islower(sPlainText[j]))
{
printf("%c",((((sPlainText[j] - 97) + iKey[k%iKeyLength])%26)+ 97));
k++;
}
if(isupper(sPlainText[j]))
{
printf("%c",((((sPlainText[j] - 65) + iKey[k%iKeyLength])%26)+ 65));
k++;
}
}
printf("\n");
return 0;
}