I am trying to make a substitution cipher but it seems to have a segmentation fault when argc does not equal 2
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
bool error(void);
int main(int argc, string argv[])
{
string key = argv[1];
int n = strlen(key);
printf("argc = %in n = %in", argc, n);
if (argc != 2 || n != 26)
{
return error();
}
for (int i = 0; i < n; i++)
{
key[i] = toupper(key[i]);
if (key[i] < 'A' || key[i] > 'Z')
{
return error();
}
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (key[i] == key[j])
{
return error();
}
}
}
string text = get_string("plaintext: ");
n = strlen(text);
for (int i = 0; i < n; i++)
{
if (isupper(text[i]))
{
text[i] = key[text[i] - 65];
}
else if(islower(text[i]))
{
text[i] = tolower(key[text[i] - 97]);
}
}
printf("ciphertext: %sn", text);
}
bool error(void) {
printf("Usage: ./substitution keyn");
return 1;
}
tried printf to display the parameters of main but exited with segmentation fault
my post is mostly code and the bot is saying that i need to add more details but not sure what else to add can someone advise
6