I've been actually checking the code for hours without figuring out what mistake it's got. The answers are basically all wrong except for the first letter, which is always right. Sometimes other letters are right as well. I can't find any sort of pattern the mistakes follow. Please help me find out what's wrong with the program.
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, string argv[])
{
string key=argv[1];
for(int i=0;i<strlen(key);i++)
{
if (key[i]>=65 && key[i]<=90)
{
key[i]=key[i]-65;
}
else if (key[i]>=97 && key[i]<=122)
{
key[i]=key[i]-97;
}
}
string plaintext = GetString();
for(int n=0;n<strlen(plaintext);n++)
{
int q=n;
if (q>strlen(key)-1)
{
while(q>strlen(key)-1)
{
q=q-strlen(key);
}
}
if ((plaintext[n]>=65&&plaintext[n]<=90)||(plaintext[n]>=97&&plaintext[n]<=122))
{
if (plaintext[n]>=65 && plaintext[n]<=90 && plaintext[n]+key[q]>90)
{
while (plaintext[n]+key[q]>90)
{
plaintext[n]=plaintext[n]-26;
}
}
else if (plaintext[n]>=97 && plaintext[n]<=122 && plaintext[n]+key[q]>122)
{
while (plaintext[n]+key[q]>122)
{
plaintext[n]=plaintext[n]-26;
}
}
plaintext[n]=plaintext[n]+key[q];
}
printf("%c",plaintext[n]);
}
printf("\n");
}