I am having an issue with the following code, no matter what I do the second if statement throws a segmentation error. The two are very similar, just flipped, and I can't figure it out. I even isolated the code and hard-coded values to make it run and make sure I know exactly what code is causing the error, but I still can't figure out why.
Edit #2: It may have something to do with the use of CS50 library's "string" type, and if so I would need a workable alternative. But if that's the case I wonder why it works for the first example (the first for loop) and not the second.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <cs50.h>
int main(void)
{
int i;
string key = "B";
string alphabet = "a";
for (i = 0; i < strlen(key); i++)
{
if (islower(key[i]) && isupper(alphabet[i]))
{
alphabet[i] = tolower(alphabet[i]);
}
}
for (i = 0; i < strlen(key); i++)
{
if (isupper(key[i]) && islower(alphabet[i]))
{
alphabet[i] = toupper(alphabet[i]);
}
printf("yeet");
}
}
Edit #1: UPDATED WITH MRE. However, I'm concerned about someone else's ability to reproduce due to the fact that this includes Harvard's CS50 library, and I don't know what the accessibility of this library is outside of the CS50 course.