So i wrote my program, but when i start it, i get the segmentation fault, yet when i start it with debug50 it works, i know what is the segmentation fault, but i can't figure out why and where would this segmentation fault happen,
I was thinking that it might happen in line 33 if the condition to reset the key letter doesn't trigger (if i reach the last letter of the key, go back to the first letter), but there are no reasons that it wouldn't
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
// Constants
#define ASCIILOWERCASE 97;
#define ASCIIUPPERCASE 65;
#define ALPHABETICALLENGTH 26;
// Functions prototypes
int charCrypter(char keyChar, char uncryptedChar);
int indexValue(char asciiChar);
int main(int argc, string argv[])
{
// Exit program if CLI is wrong
if (argc != 2 || isalpha(argv[1]) == false)
{
return 1;
}
string userKey = argv[1];
string userInputText = get_string("Text: ");
int letter = 0;
// Main loop of the program
for (int i = 0, n = strlen(userInputText); i < n; i++)
{
if (isalpha(userInputText[i]))
{
// Loop the key
if (letter == strlen(userKey))
{
letter = 0;
}
printf("%c", charCrypter(userKey[letter], userInputText[i]));
letter++;
}
else
{
printf("%c", userInputText[i]);
}
}
}
// Encrypt the character
int charCrypter(char keyChar, char uncryptedChar)
{
// get the index value
int keyIndex = indexValue(keyChar);
int userIndex = indexValue(uncryptedChar);
int alphabeticalValue = (keyIndex + userIndex) % ALPHABETICALLENGTH
// Cipher the uncrypted char
if (islower(uncryptedChar))
{
return alphabeticalValue + ASCIILOWERCASE;
}
return alphabeticalValue + ASCIIUPPERCASE;
}
// Get index value
int indexValue(char asciiChar)
{
if (islower(asciiChar))
{
return asciiChar - ASCIILOWERCASE;
}
return asciiChar - ASCIIUPPERCASE;
}