"Segmentation fault" 在 C 代码中 - 我的代码适用于加密问题
"Segmentation fault" in C code - My code works on an encryption problem
我正在研究基于给定密钥进行加密的代码。当我执行代码时,它可以顺利运行以检查命令行输入。但是,在我输入明文后,代码导致“Segmentation fault”。我查看了我的代码,但不知道出了什么问题。非常感谢任何帮助。
这是我的代码:
#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int encryption(string plaintext, int count_pt, int key);
int main(int argc, string argv[])
{
// Check if the program takes two arguments
if (argc != 2)
{
printf("%s", "The program takes two arguments.");
return 1;
}
// Count the characters of the second argument
int count = strlen(argv[1]);
// Make sure the second argument is digit only
for (int i = 0; i < count; i++)
{
if (isdigit(argv[1][i]))
{
printf("%s\n", "Success.");
// Convert the string into integer
int key = atoi(argv[1]);
// Prompt the user for plaintext
string plaintext = get_string("Please provide the plaintext: ");
int count_pt = strlen(plaintext);
int result = encryption(plaintext, count_pt, key);
printf("%d", result);
}
else if (!isdigit(argv[1][i]))
{
printf("%s\n", "Fail.");
return 1;
}
}
}
int encryption(string plaintext, int count_pt, int key)
{
int char_array[count_pt];
int ascii_shift = 0;
//Encrypt the plaintext by wrapping the characters based on the key
for (int i = 0; i < count_pt; i++)
{
//Check if characters are alphabetic
if (isalpha(plaintext))
{
if (isupper(plaintext))
{
ascii_shift = plaintext[i] - 'A';
char_array[i] = ((ascii_shift + key) % 26) + 'A';
}
if (islower(plaintext))
{
ascii_shift = plaintext[i] - 'a';
char_array[i] = ((ascii_shift + key) % 26) + 'a';
}
}
else
{
//Keep the non-alphabetical characters the same
char_array[i] = plaintext[i];
}
}
return char_array[count_pt];
}
问题出在函数加密上。最具体的是这部分...
if (isalpha(plaintext))
{
if (isupper(plaintext))
{
ascii_shift = plaintext[i] - 'A';
char_array[i] = ((ascii_shift + key) % 26) + 'A';
}
if (islower(plaintext))
{
ascii_shift = plaintext[i] - 'a';
char_array[i] = ((ascii_shift + key) % 26) + 'a';
}
}
函数 isalpha()、isupper() 和 islower() 采用 int 参数,但您给它们 plaintex 是一个指针(表示内存地址的十六进制数)。要解决此问题,您需要传递明文的字符。您可以通过两种方式完成。
- 通过索引访问字符
if (isalpha(plaintext[i]))
{
if (isupper(plaintext[i]))
{
ascii_shift = plaintext[i] - 'A';
char_array[i] = ((ascii_shift + key) % 26) + 'A';
}
if (islower(plaintext[i]))
{
ascii_shift = plaintext[i] - 'a';
char_array[i] = ((ascii_shift + key) % 26) + 'a';
}
}
- 在取消引用的帮助下
if (isalpha(*plaintext))
{
if (isupper(*plaintext))
{
ascii_shift = plaintext[i] - 'A';
char_array[i] = ((ascii_shift + key) % 26) + 'A';
}
if (islower(*plaintext))
{
ascii_shift = plaintext[i] - 'a';
char_array[i] = ((ascii_shift + key) % 26) + 'a';
}
}
我正在研究基于给定密钥进行加密的代码。当我执行代码时,它可以顺利运行以检查命令行输入。但是,在我输入明文后,代码导致“Segmentation fault”。我查看了我的代码,但不知道出了什么问题。非常感谢任何帮助。
这是我的代码:
#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int encryption(string plaintext, int count_pt, int key);
int main(int argc, string argv[])
{
// Check if the program takes two arguments
if (argc != 2)
{
printf("%s", "The program takes two arguments.");
return 1;
}
// Count the characters of the second argument
int count = strlen(argv[1]);
// Make sure the second argument is digit only
for (int i = 0; i < count; i++)
{
if (isdigit(argv[1][i]))
{
printf("%s\n", "Success.");
// Convert the string into integer
int key = atoi(argv[1]);
// Prompt the user for plaintext
string plaintext = get_string("Please provide the plaintext: ");
int count_pt = strlen(plaintext);
int result = encryption(plaintext, count_pt, key);
printf("%d", result);
}
else if (!isdigit(argv[1][i]))
{
printf("%s\n", "Fail.");
return 1;
}
}
}
int encryption(string plaintext, int count_pt, int key)
{
int char_array[count_pt];
int ascii_shift = 0;
//Encrypt the plaintext by wrapping the characters based on the key
for (int i = 0; i < count_pt; i++)
{
//Check if characters are alphabetic
if (isalpha(plaintext))
{
if (isupper(plaintext))
{
ascii_shift = plaintext[i] - 'A';
char_array[i] = ((ascii_shift + key) % 26) + 'A';
}
if (islower(plaintext))
{
ascii_shift = plaintext[i] - 'a';
char_array[i] = ((ascii_shift + key) % 26) + 'a';
}
}
else
{
//Keep the non-alphabetical characters the same
char_array[i] = plaintext[i];
}
}
return char_array[count_pt];
}
问题出在函数加密上。最具体的是这部分...
if (isalpha(plaintext))
{
if (isupper(plaintext))
{
ascii_shift = plaintext[i] - 'A';
char_array[i] = ((ascii_shift + key) % 26) + 'A';
}
if (islower(plaintext))
{
ascii_shift = plaintext[i] - 'a';
char_array[i] = ((ascii_shift + key) % 26) + 'a';
}
}
函数 isalpha()、isupper() 和 islower() 采用 int 参数,但您给它们 plaintex 是一个指针(表示内存地址的十六进制数)。要解决此问题,您需要传递明文的字符。您可以通过两种方式完成。
- 通过索引访问字符
if (isalpha(plaintext[i]))
{
if (isupper(plaintext[i]))
{
ascii_shift = plaintext[i] - 'A';
char_array[i] = ((ascii_shift + key) % 26) + 'A';
}
if (islower(plaintext[i]))
{
ascii_shift = plaintext[i] - 'a';
char_array[i] = ((ascii_shift + key) % 26) + 'a';
}
}
- 在取消引用的帮助下
if (isalpha(*plaintext))
{
if (isupper(*plaintext))
{
ascii_shift = plaintext[i] - 'A';
char_array[i] = ((ascii_shift + key) % 26) + 'A';
}
if (islower(*plaintext))
{
ascii_shift = plaintext[i] - 'a';
char_array[i] = ((ascii_shift + key) % 26) + 'a';
}
}