So, I'm trying to do a caesar cipher for CS50, and I'm modifying the contents of an array within a function. I have a for loop that goes through the string, I take a single element of the string, and I'm setting it to something, and the moment I do, I get a segmentation fault. Could anyone explain to me what's going on?
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void shift(string sentence, int number);
int main(void)
{
string sentence = "abcde"; // dummy array
int number = 1; // we are shifting it by one, so a becomes b etc
shift(sentence, number);
printf("%s\n", sentence);
}
void shift(string sentence, int number)
{
for (int i = 0, n = strlen(sentence); i < n; i++) // we loop through it from zero to the length of the string
{
if (isalpha(sentence[i]) != 0) // if it is not a letter, we skip it
{
sentence[i] = (int) sentence[i] + number; // I get a segmentation fault here
}
}
return;
}```