我正在编写一个将纯文本加密为密文的程序。当我去运行我的程序时,我收到一个分段错误,核心转储错误。
这是我的代码:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
int k = 0;
// continues the program if 2 and no more than 2 command line arguments exist and argv[1] contains alphabetical characters only
if(argc == 2 && argc == isalpha(argv[1]))
{
k = atoi(argv[1]);
}
// re-prompts user to enter only 2 command line arguments and the second should consist of only alphabetical characters
else
{
printf("You must enter a command line argument using only alphabetical characters!\n");
return 1;
}
string text = GetString();
int cipher;
int key;
// loops through each charcter in key and gives each character a valule to add to plain text
for (int j = 0, n = strlen(argv[1]); j < n; j++)
{
if (isalpha(argv[1][j]))
{
if (isupper(argv[1][j]))
{
key = 26 - (91 - argv[1][j]);
}
else
{
key = 26 - (123 - argv[1][j]);
}
}
else
{
key = argv[1][j];
}
// loops through plaintext entered by user and changes text to ciphertext according to key
for (int i = 0, l = strlen(text); i < l; i++)
{
if (isalpha(text[i]))
{
cipher = (text[i] + key) % n;
printf("%c", cipher);
}
else
{
printf("%c", text[i]);
}
}
}
printf("\n");
}