=CS50 PSET 2 CAESAR= 如何将密钥转换为数字?如果我使用 atoi() ,它会给我这个错误

发布于 2025-01-10 02:46:12 字数 2502 浏览 6 评论 0原文

我在 only_digits 函数中遇到 atoi() 问题。我在 Discord 上询问,他们说我将 char 类型 arg 传递给 atoi() ,这不起作用,因为 atoi() 只接受 string 或 char * (array char) 作为参数。我不明白。我对字符串和字符的区别感到困惑。我不是将 argv[1] (这是一个字符串)传递给 only_digits 吗?这意味着 inputKey 也是一个字符串?那么我将 char 类型 arg 传递给 atoi() 意味着什么?我究竟如何使 atoi() 工作?我被这个问题困扰了两天了。

// Encrypts text using Caesar's cipher
// ci = (pi + k) % 26

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

bool only_digits (string inputKey);
char rotate (char plaintext[], int key);

int main(int argc, string argv[])
{
    string p;

    // Make sure program was run with just one command-line argument

    if (argc != 2)
    {
        printf("Enter exactly one input\n");
        return 1;
    }

    // Make sure every character in argv[1] is a digit (DOESN'T WORK)

    /*else if (only_digits(argv[1]))
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }*/

    else
    {
        p = get_string("plaintext: ");
    }

    // Convert argv[1] from a string to an int

    int k = atoi(argv[1]);
    char c[strlen(p) + 1];

    // Convert ASCII range down to a value from 0 to 25



    // For each character in the plaintext: (DOESN'T WORK)

    for (int i = 0, n = strlen(p); i <= n; i++)
    {
        // Rotate the character if it's a letter // ci = (pi + k) % 26

        if (isalpha(p[i]))
        {
            if (isupper(p[i]))
            {
                c[i] = ((p[i]) + k) % 26;
            }
            else if (islower(p[i]))
            {
                c[i] = ((p[i]) + k) % 26;
            }
        }
    }

    printf("ciphertext: %s\n", c);
}

// Function to encrypt plaintext

/*char rotate (char plaintext[], int key)
{
    char c[strlen(plaintext) + 1];

    return c;
}*/

// Function to check if key is a digit (DOESN'T WORK)

bool only_digits (string inputKey)
{
    int flag = 0;

    for (int i = 0, n = strlen(inputKey); i < n; i++)
    {
        // Converts string input to int

        int response = atoi(inputKey[i]);

        // Check if it is a digit

        if (!(isdigit(response)))
        {
            flag++;
        }
    }

    if (flag != 0)
    {
        return false;
    }
    else
    {
        return true;
    }
}

语法错误

I'm having a problem with atoi() in the only_digits function. I asked on discord and they said that I am passing a char type arg to atoi() which doesn't work since atoi() only takes string or char * (array of char) as arguments. I don't get it. I'm confused with the difference of string and char. Aren't I passing argv[1] (which is a string) to only_digits? Which means inputKey is a string as well? So what do they mean that I am passing a char type arg to atoi()? How exactly do I make atoi() work? I'm stuck with this problem for 2 days now.

// Encrypts text using Caesar's cipher
// ci = (pi + k) % 26

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

bool only_digits (string inputKey);
char rotate (char plaintext[], int key);

int main(int argc, string argv[])
{
    string p;

    // Make sure program was run with just one command-line argument

    if (argc != 2)
    {
        printf("Enter exactly one input\n");
        return 1;
    }

    // Make sure every character in argv[1] is a digit (DOESN'T WORK)

    /*else if (only_digits(argv[1]))
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }*/

    else
    {
        p = get_string("plaintext: ");
    }

    // Convert argv[1] from a string to an int

    int k = atoi(argv[1]);
    char c[strlen(p) + 1];

    // Convert ASCII range down to a value from 0 to 25



    // For each character in the plaintext: (DOESN'T WORK)

    for (int i = 0, n = strlen(p); i <= n; i++)
    {
        // Rotate the character if it's a letter // ci = (pi + k) % 26

        if (isalpha(p[i]))
        {
            if (isupper(p[i]))
            {
                c[i] = ((p[i]) + k) % 26;
            }
            else if (islower(p[i]))
            {
                c[i] = ((p[i]) + k) % 26;
            }
        }
    }

    printf("ciphertext: %s\n", c);
}

// Function to encrypt plaintext

/*char rotate (char plaintext[], int key)
{
    char c[strlen(plaintext) + 1];

    return c;
}*/

// Function to check if key is a digit (DOESN'T WORK)

bool only_digits (string inputKey)
{
    int flag = 0;

    for (int i = 0, n = strlen(inputKey); i < n; i++)
    {
        // Converts string input to int

        int response = atoi(inputKey[i]);

        // Check if it is a digit

        if (!(isdigit(response)))
        {
            flag++;
        }
    }

    if (flag != 0)
    {
        return false;
    }
    else
    {
        return true;
    }
}

syntax error

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

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

发布评论

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

评论(1

债姬 2025-01-17 02:46:12

C 中的字符串被定义为非零字节(字符)序列,后跟一个空终止字节:

char str[] = "hello"; /* in memory: { 'h', 'e', 'l', 'l', 'o', '\0' } */

atoiASCII 到整数)需要正确的以 null 结尾的字符串 (<代码>字符*)。您不能向其传递单个char

int number = atoi("1672387");

isdigit 需要一个 < em>角色。

int is_true = isdigit('5');

在您的程序中,inputKey[i] 是一个字符。您可以直接使用isdigit进行测试,无需事先将其转换为整数表示。

如果遇到非数字字符,您也可以提前返回。

bool only_digits(string inputKey) {
    for (size_t i = 0, length = strlen(inputKey); i < length; i++)
        if (!isdigit(inputKey[i]))
            return false;

    return true;
}

注意:size_tstrlen的返回类型,也是最适合索引内存的类型。

Strings in C are defined as a sequence of nonzero bytes (characters), followed by a null-terminating byte:

char str[] = "hello"; /* in memory: { 'h', 'e', 'l', 'l', 'o', '\0' } */

atoi (ASCII to integer) expects a proper null-terminated string (char *). You can not pass it a single char.

int number = atoi("1672387");

isdigit expects a single character.

int is_true = isdigit('5');

In your program inputKey[i] is a single character. You can test it directly with isdigit, there is no need to convert it to an integer representation beforehand.

You can also simply return early if you encounter a non-digit character.

bool only_digits(string inputKey) {
    for (size_t i = 0, length = strlen(inputKey); i < length; i++)
        if (!isdigit(inputKey[i]))
            return false;

    return true;
}

Note: size_t is the return type of strlen, and the most appropriate type for indexing memory.

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