I have a program here where I'm trying to decode a string of letters using a ceasar cipher; essentially I'm moving each character in the string "down" a letter ("a" -> "b", "f" -> "g", "z" -> "a").
The amount that I move a letter down depends on the key I give it.
In this specific program, I have a secret coded message hardcoded into the main() function, and a for loop iterating through each possible key.
The idea is that if this secret message is simply shifted downward by x amount of letters, spitting out 25 versions of the cipher will reveal an intelligible answer.
Unfortunately I'm using some concepts that are new to me - argc, argv, and multiple functions in one program. I am very new to this.
Can someone help explain the segmentation fault error I'm getting? I don't think I'm causing any overflows on arguments.
#include <stdio.h>
#include <cs50.h>
#include <string.h>
string decode(int key, string message);
int main(void)
{
string secret_message = "ueuag rKJ AGIQ GIAR FEgN";
for (int i = 0; i < 25; i++)
{
decode(i, secret_message);
printf("%s", secret_message);
}
return 0;
}
string decode(int key, string message)
{
int i;
for (i = 0; i < strlen(message); i++)
{
if (message[i] >= 'a' && message[i] <= 'z')
{
message[i] = ((message[i] - 97 + key) % 26) + 97;
}
else if (message[i] >= 'A' && message[i] <= 'Z')
{
message[i] = ((message[i] - 65 + key) % 26) + 65;
}
}
return message;
}