My Vigenere cipher has problems. When I input my message, the result comes out fine, but if the letter goes past 'z', it doesn't loop back to 'a', and is printing out other ascii characters. Moreover, when I put the message in, I sometimes get more characters than I needed. ex: key is hello, message is mmmmm(I know, not much of a message, but it's an example), and the output is tqxx{{. PLEASE HELP!!!!
#include<cs50.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
int main( int argc, char *argv[])
{
char *k;
if (argc!=2)
{
printf("Give one argument, not 2, not 3, not 4 , and not any other amount other than one");
printf("\n");
return 1;
}
k = argv[1];
printf("What is the message? ");
string message = get_string();
if (strlen(k)<strlen(message))
{
printf("Invalid response\n");
return 0;
}
for (int i = 0, l = 0; i < strlen(message); i++, l++)
{
int x;
if (message[i]>='A' && message[i] <='Z')
{
message[i] = message[i]-'A';
message[i] = message[i] + (k[l] - 'A') % 26;
message[i] = message[i] + 'A';
message[i] = (char) x;
if (x > 90)
{
x = x - 26;
message[i] = (char) x;
}
printf("%c", message[i]);
}
if(message[i] >='a' && message[i] <= 'z')
{
message[i]= message[i]-'a';
message[i] = message[i] + (k[l] - 'a') % 26;
message[i] = message[i] + 'a';
printf("%c", message[i]);
}
if ((message[i] < 'A') || (message[i] > 'z') || (message[i] > 'Z' && message[i] < 'a'))
{
printf("%c", message[i]);
}
}
printf("\n");
return 0;
}