此功能中有什么错误? (初学者CS50)

发布于 2025-02-04 06:12:19 字数 1735 浏览 0 评论 0原文

我正在尝试使用此功能将给定文本(字符串A)用给定的字母(字符串B)加以关注。 它编译和运行,实际上替换字母不是正确的方式。

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

string replace(string a , string b);
int main(int argc, string argv[])
{
    string code;
    if(argc != 2)
    {
        printf("Usage: ./substitution key\n");
        return 1;
    }
    else
    {
        code = argv[1];
        if(strlen(argv[1]) != 26)
        {
            printf("Key must contain 26 characters.\n");
            return 1;
        }
        for(int i = 0; i < 26; i++)
        {
            if (isalpha(code[i]) == 0)
            {
                printf("Key must only contain alphabetic characters.\n");
                return 1;
            }
        }
        for(int j = 0; j < 26; j++)
        {
            for(int n = j + 1; n < 26; n++)
            {
                if(code[j] == code[n])
                {
                    printf("Key must not contain repeated characters.\n");
                    return 1;
                }
            }
        }
    }
    string txt = get_string("plaintext:");
    printf("ciphertext: %s\n" , replace(txt , code));
    return 1;
}

string replace(string a , string b)
{
    string final = a;
    string alpha = {"abcdefghijklmnopqrstuvwxyz"};
    for (int i = 0; i < strlen(a); i++)
    {
        for(int n = 0; n < 26; n++)
        {
            if(a[i] == alpha[n])
            {
                final[i] = b[n];
            }
        }
    }
    return final;
}

我只想了解这是我的一些示例:

./substitution ytnshkvefxrbauqzclwdmipgjo
plaintext: hello
ciphertext hhbbq

当我再尝试一次,输出为ehbbq时,这是正确的,但是每次我尝试时,它都不一样。

i'm trying to cipher a given text (string a) with a given set of letters (string b) using this function.
it compiles and runs and actually replaces the letters just not the right way.

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

string replace(string a , string b);
int main(int argc, string argv[])
{
    string code;
    if(argc != 2)
    {
        printf("Usage: ./substitution key\n");
        return 1;
    }
    else
    {
        code = argv[1];
        if(strlen(argv[1]) != 26)
        {
            printf("Key must contain 26 characters.\n");
            return 1;
        }
        for(int i = 0; i < 26; i++)
        {
            if (isalpha(code[i]) == 0)
            {
                printf("Key must only contain alphabetic characters.\n");
                return 1;
            }
        }
        for(int j = 0; j < 26; j++)
        {
            for(int n = j + 1; n < 26; n++)
            {
                if(code[j] == code[n])
                {
                    printf("Key must not contain repeated characters.\n");
                    return 1;
                }
            }
        }
    }
    string txt = get_string("plaintext:");
    printf("ciphertext: %s\n" , replace(txt , code));
    return 1;
}

string replace(string a , string b)
{
    string final = a;
    string alpha = {"abcdefghijklmnopqrstuvwxyz"};
    for (int i = 0; i < strlen(a); i++)
    {
        for(int n = 0; n < 26; n++)
        {
            if(a[i] == alpha[n])
            {
                final[i] = b[n];
            }
        }
    }
    return final;
}

i just want to understand whats up with it here are some of my samples:

./substitution ytnshkvefxrbauqzclwdmipgjo
plaintext: hello
ciphertext hhbbq

and when i try it one more time that output is ehbbq which is the right one but every time i try it it's different.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文