I want to create the César cipher, also known as offset encryption, caesar.c
with C. I receive the number in the variable as an argument ./caesar 2
for example. I use it in key
. In an array of String s
I receive the text without format. In encryptedASCII[strlen(s)]
I will make the changes.
However, I receive a Segmentation fault
. I thought it was because the size of the matrix was assigned dynamically but the error persisted when it did encryptedASCII[1000]
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, string argv[])
{
int key = atoi(argv[2]);
if (key<0)
{
return 1;
}
printf("¿Que quiere cifrar?\n");
string s = GetString();
int encryptedASCII[strlen(s)];
char encryptedText[strlen(s)];
for (int i = 0, n = strlen(s); i < n; i++)
{
if (isalpha(s[i]))
{
encryptedASCII[i] = ('s[i]' +key)%26;
encryptedText[i]=encryptedASCII[i];
}
}
//Aqui estamos fijando el texto cifrado
for (int i = 0, n = strlen(s); i < n; i++)
{
printf("%c", encryptedText[i]);
}
return 0;
}