这是哈佛CS50第2,https://cs50.harvard.edu/x/2023/psets/2/substitution/周的问题.

它给了我错误

将‘char’传递给‘const char*’类型的参数的指针转换的整数不兼容;

It occurs in line 36 when I try to compile the program. I am using the cs50.dev to run the vs code in browser. From here you can use the <cs50.h> enter image description here

我怎么才能修好它呢?

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

int main(int argc, string argv[])
{
    string pt = get_string("plaintext: ");
    int len = strlen(pt);
    string ct;;
    string key = argv[1];
    int lenkey = strlen(key);
    if (lenkey < 26)
    {
        printf("Key must contain 26 characters.");

    }
    else if (argc > 2)
    {
        printf("Usage: ./substitution key");

    }
    else if (argc < 2)
    {
        printf("Usage: ./substitution key");

    }
    string keyu;
    string keyl;
    // Key of uppercase
    for (int i = 0; i < 26; i++)
    {
        if (isupper(key[i]))
        {
            keyu = strcat(keyu, key[i]);
        }
        else if (islower(key[i]))
        {
            keyu = strcat(keyu, toupper(key[i]));
        }
    }
    // key of lowercase
    for (int i = 0; i < 26; i++)
    {
        if (islower(key[i]))
        {
            keyl = strcat(keyl, key[i]);
        }
        else if (isupper(key[i]))
        {
            keyl = strcat(keyl, toupper(key[i]));
        }
    }
    // create cyphertext
    for (int i = 0; i < len; i++)
    {
        if (islower(pt[i]))
        {
            ct = strcat(ct, keyl[pt[i] - 97]);
        }
        else if (isupper(pt[i]))
        {
            ct = strcat(ct, keyu[pt[i] - 65]);
        }
        else
        {
            ct = strcat(ct, pt[i]);
        }
    }
}

推荐答案

正如Good Comments中所指出的,该错误的主要问题是"strcat"函数被设置为将一个字符数组(字符串)连接到另一个字符数组(字符串).在您的代码中,它正在try 将一个字符连接到一个字符array.

以下是您的程序的重构版本,它基于加密密钥构建Word的加密版本.

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

int main(int argc, char *argv[])                        /* Usual declaration with input parameters              */
{
    if (argc != 2)
    {
        printf("Usage: ./substitution key\n");
        return 1;
    }
    char ct[30], pt[30], key[30], keyl[30], keyu[30];   /* Strings are character arrays                         */
    int len;
    //string pt = get_string("plaintext: ");
    printf("plaintext: ");                              /* Common method to prompt for input                    */
    len = scanf("%s", pt);
    len = strlen(pt);

    //string ct;;
    //string key = argv[1];

    strcpy(key, argv[1]);                               /* Common string copy method                            */
    int lenkey = strlen(key);
    if (lenkey < 26)
    {
        printf("Key must contain 26 characters.");
        return 2;
    }

    //string keyu;
    //string keyl;

    // Key of uppercase
    for (int i = 0; i < 26; i++)                        /* Simply store uppercase versions of the characters    */
    {
        keyu[i] = toupper(key[i]);
    }
    // key of lowercase
    for (int i = 0; i < 26; i++)                        /* Simply store lowercase versions of the characters    */
    {
        keyl[i] = tolower(key[i]);
    }
    // create cyphertext
    for (int i = 0; i < len; i++)
    {
        if (islower(pt[i]))
        {
            ct[i] = keyl[pt[i] - 97];
        }
        else if (isupper(pt[i]))
        {
            ct[i] = keyu[pt[i] - 65];
        }
        else
        {
            ct[i] = pt[i];
        }
    }

    printf("cypher text: %s\n", ct);

    return 0;
}

此重构代码的一些关键点是:

  • 因为我的系统上没有安装"CS50"库和文件,所以专门利用该上下文中的函数和定义的代码被注释掉了,取而代之的是泛型定义(例如,pt被定义为字符数组,而不是定义"字符串pt").
  • 构建大写和小写加密密钥实际上不需要进行小写或大写测试,因此这使得那些"for循环"变得更简单.
  • 加密文本中的每个字符都是使用key参数逐个字符构建的,而不是使用"strcat"函数.

以下是对重构后的代码的一些测试.

craig@Vera:~/C_Programs/Console/Substitution/bin/Release$ ./Substitution zyxwvytsrqponmlkjihgfedcba
plaintext: checkers
cypher text: xsvxpvih
craig@Vera:~/C_Programs/Console/Substitution/bin/Release$ ./Substitution zyxwvytsrqponmlkjihgfedcba
plaintext: xsvxpvih
cypher text: checkers
craig@Vera:~/C_Programs/Console/Substitution/bin/Release$ ./Substitution Hello there
Usage: ./substitution key
craig@Vera:~/C_Programs/Console/Substitution/bin/Release$ 

我绝不是在贬低"CS50"支持库和文件.在你的学习中使用你认为合适的功能.但是,当您判断来自其他来源的代码时,您可能会看到字符数组定义和其他变量定义等项更有可能采用这种常见格式.因此,您还需要熟悉以这种方式构建的代码.

C++相关问答推荐

Pure Win32 C(++)-除了替换控件的窗口程序之外,还有其他方法可以在输入时禁用按钮吗?

我可以动态分配具有空类型函数的矩阵吗?

Apple Libm的罪恶功能

C指针算法在函数参数中的应用

如何在C中通过套接字自定义数据类型读取原始变量?

字符是否必须转换为无符号字符,然后才能与getc家族的返回值进行比较?

为什么我会收到释放后堆使用错误?

获取每个循环迭代结束时的当前时间

为什么我从CSV文件中进行排序和搜索的代码没有显示数据的所有结果?

编译器如何处理具有更复杂值的枚举?

S和查尔有什么不同[1]?

收到不兼容的指针类型警告,因为函数的返回不是空*,而是 struct 指针

Printf()在C中打印终止字符之后的字符,我该如何解决这个问题?

如何在C中定义指向函数的指针并将该指针赋给函数?

将某些内容添加到链接列表时,列表中的其他项将使用最后添加的项的名称

在同一范围内对具有相同类型的变量执行的相同操作在同一C代码中花费的时间不同

使用 c 中的 write() 函数将非 ASCII 字符写入标准输出

为什么<到达*时不会转换为>?

如何用用户输入的多个字符串填充数组?

在链表中插入一个值