I think the problem is in the loops, probably something wrong with my ASCII math, I've tried everything I could but I can't comeup with a solution in 9 hours of trying, I need help...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <cs50.h>
int main(int argc, string argv[])
{
/* Program that encrypts user message, through Vigenere's Cypher
* using the desired keyword
*/
// Input Validation
if (argc == 2)
{
string keyword = argv[1];
int keywordLenght = strlen(keyword);
for (int i = 0 ; i < keywordLenght ; i++)
{
if (!isalpha(keyword[i]))
{
printf("Invalid Keyword.\n");
return 2;
}
}
// Gets the text to be enciphered
string text = GetString();
// Enciphers the text using the desired key
int textLenght = strlen(text);
int key;
for (int n = 0 ; n < textLenght ; n++)
{
if (isalpha(text[n]))
{
if (isupper(text[n]))
{
key = toupper(keyword[n % keywordLenght]);
text[n] = text[n] + ('A' - key) % 26;
}
else
{
key = tolower(keyword[n % keywordLenght]);
text[n] = text[n] + ('a' - key) % 26;
}
}
else
text[n] = text[n];
}
printf("%s\n", text);
}
else
{
printf("Invalid Input.\n");
return 1;
}
}