I am new to programming and I am stuck with Pset2 for a week as it does not seem to do the right thing no matter how I modify it. Can you please give me a hint of what's wrong with the code?
int main(int argc, string argv[])
{
// check for 2 arguments only
if (argc != 2)
{
printf("Enter the key please \n");
return 1;
}
// check for correct argv put key into k
string key = argv[1];
// check key is text
for (int j=0, n=strlen(key); j<n; j++)
{
if (!isalpha(key[j]))
printf("Enter only alphabetical key.");
}
{
// prompt user for a code to encrypt
string msg = get_string("cipher text: ");
//obtain length of key and turn it to lower case integer
int kLength = strlen(key);
for (int i = 0, j = 0, n = strlen(msg); i < n; i++)
{
//check if the letter is uppercase or lowercase then convert, use "-97" to turn key ASCII to alphabetical order
if islower(msg[i])
{
printf("%c", ((msg[i] +tolower(key[j]) - 97- 97) % kLength) + 97);
j++;
}
else if isupper(msg[i])
{
printf("%c", ((msg[i] + tolower(key[j]) - 65- 97) % kLength) + 65);
j++;
}
//increment j only if used
else
//if neither then just print whatever it is
printf("%c", msg[i]);
}
printf("\n");
return 0;
}
}