I'm trying to make a Vigenère encryption code for my problem set of the week and I'm almost done. I have a little problem, I can't make the keyword char shift only on alphabetical chars this is my code and I can't track the problem.
GetString() is implemented by library (it's like scanf) & string typedef also
int main(int argc, string argv[])
{
string keyWord;
if( argc != 2 )
{
printf("Wrong Argument");
return 1;
}
else
{
keyWord = argv[1];
//check if argument is
//only alphabetical characters
for(int i = 0; i < strlen(keyWord); i++)
{
char c = keyWord[i];
if( !isalpha(c) )
{
printf("Your Keyword Must Contain Only alphabetical characters\n");
return 1;
}
}
}
string plainText = GetString();
for(int i = 0,j = 0; i < strlen(plainText); i++,j++)
{
if(j >= strlen(keyWord))
j = 0;
char c = plainText[i];
int keyWordWrapper;
char keyC;
if(isalpha(c))
{
keyWordWrapper = j % strlen(keyWord);
keyC = keyWord[keyWordWrapper];
int key;
tolower(c);
if(islower(keyC))
key = keyC - 'a';
if(isupper(keyC))
key = keyC - 'A';
c = (c - 'a' + key) % 26 + 'a';
}
printf("%c",c);
}
printf("\n");
return 0;
}
you may do the following
//to skip the spaces
if(c==32)
{
j--;
continue;
}
char c = plainText[i];
int keyWordWrapper;
char keyC;
if(isalpha(c))
{
keyWordWrapper = j % strlen(keyWord);
keyC = keyWord[keyWordWrapper];
int key;
tolower(c);
if(islower(keyC))
{
key = keyC - 'a';
c = (c - 'a' + key) % 26 + 'a';
}
if(isupper(keyC))
{
key = keyC - 'A';
c = (c - 'A' + key) % 26 + 'A';
}
}