I'm getting a segmentation error as soon as I execute this program. I've run it through the debugger and it works fine and outputs the correct ciphertext. There's gotta be something going on in the first few lines but I can't figure out what.
If it helps, when I execute, in the debugger, it lists (what I imagine is) an unusual value for argv:
Code is below. Can anyone point me in the right direction?
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
if (argc ==2 && isdigit(argv[1])) //if argc = 2 AND argv[1] is digits
{
int k = atoi(argv[1]); //initialize k as the conversion of argv[1] to an integer
string plaintext = get_string("Plaintext: "); //get user input for plaintext
printf("Ciphertext: ");
for (int i = 0, len = strlen(plaintext); i < len; i++) //i is the current character its looking at
{
if (plaintext[i] >= 'a' && plaintext[i] <= 'z') //if a or higher, or z or lower
{
printf("%c", (((plaintext[i] - 'a') + k) % 26) + 'a'); //then print letter + key
}
else if (plaintext[i] >= 'A' && plaintext[i] <= 'Z') //if A or higher, or Z or lower
{
printf("%c", (((plaintext[i] - 'A') + k) % 26) + 'A'); //then print letter + key
}
else
{
printf("%c", plaintext[i]); //otherwise print whatever character is there
}
}
printf("\n");
return 0;
}
}