目標實際上是將明文字串中的字符替換為密文。用戶使用命令列引數輸入鍵,鍵輸入為 26 個字母。
我在運行程式時遇到了問題,它得到了Segmentation fault (core dumped). 在除錯期間,代碼在功能行停止作業。我的問題是發生了什么以及如何解決這個問題以便我可以創建一串鍵?
這是我的代碼行:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Declare crypting function
string encrypt(string text, string key_upper, string key_lower);
string valid_key_upper(string key);
string valid_key_lower(string key);
int main(int argc, string argv[])
{
// Must contain an argument
if (argc > 2 || argc < 2)
{
printf("Usage: ./substitution KEY\n");
return 1;
}
// take the input from the commandline and validate them.
string key_before = argv[1];
int key_length = strlen(key_before);
// evaluate the key length
if (key_length != 26)
{
printf("Key must contain 26 characters.\n");
return 1;
}
// Create initial key container
char key[26];
int evaluated_key = 0;
for (int i = 0; i < key_length; i )
{
// Validate so that only letters
if (key_before[i] < 65|| key_before[i] > 122 || (key_before[i] > 90 && key_before[i] < 97))
{
printf("Must only contain letters!\n");
return 1;
}
// Compare the current evaluated key to the existing key in the memory
else
{
for (int n = 1; n < evaluated_key; n )
{
if (key_before[i] == key[n])
{
printf("Must not contain duplicate!\n");
return 1;
}
}
// copy valid key to the key container
key[i] = key_before[i];
evaluated_key = evaluated_key 1;
}
}
// Make lower-case and upper-case function container
string key_upper = valid_key_upper(key);
string key_lower = valid_key_lower(key);
// get user input of plaintext
string plaintext = get_string("Plaintext: ");
// function for ciphering
string ciphertext = encrypt(plaintext, key_upper, key_lower);
// print out the ciphered text
printf("Ciphertext = %s\n", ciphertext);
}
string valid_key_upper(string key)
{
// Declare variable container
string key_upper = NULL;
// Take the key and evaluate each character
for (int i = 0; i < 26; i ) // evaluate for 26 characters
{
if (key[i] >= 65 && key[i] <= 90)
{
key_upper[i] = key[i];
}
else if (key[i] >= 97 && key[i] <= 122)
{
key_upper[i] = toupper(key[i]);
}
}
key_upper[26] = '\0';
return key_upper;
}
uj5u.com熱心網友回復:
您的問題在于key_upperin valid_key_upper; 沒有為字串分配空間,并且您將其初始化為NULL,因此當您將值分配給 時key_upper[i],您會丟棄堆疊。您必須malloc key_upper先將字串元素分配給它。
uj5u.com熱心網友回復:
您正在使用空指標來訪問記憶體。
例如,在函式valid_key_upper中,指標key_upper設定為 null
string key_upper = NULL;
然后在下面的 for 回圈中,您嘗試取消參考指標
// Take the key and evaluate each character
for (int i = 0; i < 26; i ) // evaluate for 26 characters
{
if (key[i] >= 65 && key[i] <= 90)
{
key_upper[i] = key[i];
//...
這會導致未定義的行為。
不需要創建這樣的字串。加密字串時,您可以將當前字符轉換為大寫或小寫。
一般來說,您的代碼可以簡化。例如,而不是這個 if 陳述句
if (argc > 2 || argc < 2)
{
printf("Usage: ./substitution KEY\n");
return 1;
}
你可以寫
if (argc != 2)
{
printf("Usage: ./substitution KEY\n");
return 1;
}
65此外,使用和之類的幻數也是一個壞主意122。而是使用整數字符常量。這使代碼更清晰。
uj5u.com熱心網友回復:
謝謝大家的回答,所以我malloc在key_upper變數中使用并解決了問題。在變數中分配了 27 個位元組。此外,我還簡化了功能。
string valid_key_upper(string key)
{
// Declare variable container
string key_upper = malloc(27);
// Take the key and evaluate each character
for (int i = 0; i < 26; i ) // evaluate for 26 characters
{
key_upper[i] = toupper(key[I]);
}
return key_upper;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/490679.html
