如何将字符串返回到主函数?

发布于 2025-02-07 04:00:17 字数 2759 浏览 1 评论 0原文

目标实际上是将字符串中的字符替换为ciphertext。用户使用命令行参数输入键,其中包含26个字母的键输入。

我运行程序时遇到了问题,它有分割故障(Core dustmed)。在调试期间,代码停止在功能线上工作。我的问题是发生了什么以及如何解决此问题,以便我可以创建一串键?

这是我的代码行:

#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;
}

The goal is actually substituting characters in a string of plaintext to ciphertext. User input the key using the command line argument with the key input of 26 letters.

I encountered problem when I run the program, it got Segmentation fault (core dumped). During the debug the code stops working at the function line. My question is what is happening and how to solve this so that I can create a string of keys?

Here is my code lines:

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灯角 2025-02-14 04:00:17

您的问题是key_upper in 有效_key_upper;没有为字符串分配空间,然后将其初始化为null,因此当将值分配给key_upper [i]时,您就会丢弃堆栈。您必须malloc key_upper才能为其分配字符串元素。

Your problem is with key_upper in valid_key_upper; no space is allocated for the string, and you initialize it to NULL, so that when when you assign values to key_upper[i], you trash the stack. You must malloc key_upper before you can assign string elements to it.

爱,才寂寞 2025-02-14 04:00:17

您正在使用空指针来访问内存。

例如,在函数中,有效_key_upper指针key_upper设置为null,

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];
        //...

导致不确定行为的指针。

无需创建这样的字符串。当字符串加密时,您可以将当前字符转换为上部或下情况。

通常,您的代码可以简化。例如,如果语句

if (argc > 2 || argc < 2)
{
    printf("Usage: ./substitution KEY\n");
    return 1;
}

可以编写,

if (argc !=  2)
{
    printf("Usage: ./substitution KEY\n");
    return 1;
}

而是使用65122之类的魔术数字是一个坏主意。而是使用整数字符常数。这使代码更加清晰。

You are using null pointers to access memory.

For example within the function valid_key_upper the pointer key_upper is set to null

string key_upper = NULL;

then in the following for loop you are trying to dereference the pointer

// 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];
        //...

that results in undefine behavior.

There is no need to create such a string. You can convert a current character to upper or to lower case when a string is encrypted.

In general your code can be simplified. For example instead of this if statement

if (argc > 2 || argc < 2)
{
    printf("Usage: ./substitution KEY\n");
    return 1;
}

you could write

if (argc !=  2)
{
    printf("Usage: ./substitution KEY\n");
    return 1;
}

Also it is a bad idea to use magic numbers like 65 and 122. Instead use integer character constants. That makes the code more clear.

罪#恶を代价 2025-02-14 04:00:17

感谢您的答案,因此我在key_upper变量中使用了malloc并解决了问题。在变量中分配了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;
}

Thank you for everybody's answer, so I used malloc in the key_upper variable and solved the problem. Allocated 27 bytes in the variable. Also, I have simplified the function.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文