Hello. I am trying to make a C program that for argv 2 the user enters a word and for argv 3 the user MUST specify whether they want the program to decrypt or encrypt the message. Program compiles fine but when I try to run it, I am getting a "segmentation fault" any ideas?
Thank you
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
int i;
const char string = argv[1]; //here I set the entered string for argv[2]
const int length = strlen(string);
char encrypted_string[length]; //length of encrypted value
char decrypted_string[length];
const char *key_ch = argv[2];
char key_int = atoi(key_ch);
if(argc < 3) //argument checking
{
printf("Not enough arguments\n");
exit (1);
}
if (strcmp(argv[3], "encrypt") == 0) //argv[3] sees if it is encrypt or decrypt
{
i = 0;
while(i <= strlen(string)-1)
{
encrypted_string[i] = string[i] + key_int;
i++;
}
// printf("Encrypted string: "); //print encrypted string
i = 0;
while (i <= strlen(string) -1)
{
printf("%c", encrypted_string[i]);
i++;
}
printf("\n");
}
if (strcmp(argv[3], "decrypt") == 0)
{
i = 0;
while(i <= strlen(string) -1)
{
decrypted_string[i] = string[i] - key_int;
i++;
}
// printf("Decrypted String: "); //print decrypted string
i = 0;
while (i <= strlen(string) -1)
{
printf("%c", decrypted_string[i]);
i++;
}
printf("\n");
}
return 0;
}