i need help in the sense that if i run my program with the key being one letter, whether capital or lowercase, it works properly, but if more than one letters in the key, the last letter is used for all the letters in the plaintext. why? because, using printf, i found out that the loop for finding if the jth char of key is letter or not, then i tell the var key_val that it == equals key[j] but once the for loop for that is done, instead of going to the next for loop (plaintext) and coming back for this loops second stage, it completly finishes this key loop (rendering it to the last letter as key_val) and than the plaintext value gets to use the last char for everything.
my code...
#include<stdio.h>
#include<cs50.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int key_val;
int main(int argc, string argv[])
{
if (argc != 2) {
printf("type program name and key! no more and no less!");
return 1;
}
string key = argv[1];
string plaintext = GetString();
for(int j = 0,n = strlen(key);j < n; j++ )
{
key_val = key[j];
if (isalpha((unsigned char)key[j])){
if (islower((unsigned char)key[j]))
{
key[j] = key[j] - 104;
}
else if (isupper((unsigned char)key[j]))
{
key[j] = key[j] - 72;
}
}
else if (!isalpha((unsigned char)key[j])){
printf("Keyword must only contain letters A-Z and a-z\n");
return 1;
}
}
for(int i = 0,n1 = strlen(plaintext);i < n1; i++ )
{
int upalpha = plaintext[i] - 'A';
int lowalpha = plaintext[i] - 'a';
int resultupper = ((upalpha + key_val + 7) % 26 + 'A');
int resultlower = ((lowalpha + key_val + 7) % 26 + 'a');
if (isalpha((unsigned char)plaintext[i])){
if (islower((unsigned char)plaintext[i]))
{
printf("%c", resultlower);
}
if (isupper((unsigned char)plaintext[i]))
{
printf("%c", resultupper);
}
}
else {
printf("%c", plaintext[i]);
}
}
printf("\n");
}