CS50 Caesar - Check50 不会验证我的代码

发布于 2025-01-12 22:50:42 字数 1324 浏览 3 评论 0原文

我知道这个问题之前已经被问过,但我仍然无法在我的代码中找到问题的线索。 我的程序显然运行良好,但我无法通过 check50 测试。据我了解,该问题可能与打印 null \0 有关。但我不知道如何修改它。请你帮助我好吗?

这是我的代码:

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

bool only_digits(string s);
char rotate(char c, int n);
int main(int argc, string argv[])
{
string h = argv[1];
if (argc != 2 || !only_digits(h) || h <= 0)
    {printf("Usage: ./caesar key\n");
    return 1;
    }
else
{
    int key = atoi(argv[1]);

string plaintext = get_string("plaintext:  ");

    int f = strlen(plaintext);
    printf("ciphertext: ");
    for(int q = 0; q < f; q++)
    {
        printf("%c", rotate(plaintext[q], key));
    }
    printf("\n");
}
    return 0;
}


bool only_digits(string s )
{
    for (int i = 0, n = strlen(s); i < n; i++)
{
    char digit = s[i];
    if (!isdigit(digit))
    return false;
}
return true;
}

char rotate(char c, int n)
{
    if(isupper(c) && (c != '\0'))
{
    printf("%c", (((c - 65) + n) % 26) + 65);
}
    else
    if(islower(c) && (c != '\0'))
    {
        printf("%c", (((c - 97) + n) % 26) + 97);
    }
    else
    printf("%c", c);
return 0;
}

这是 check50 支票的标题: check50

I know that this question have been asked before, but I still can't find the clue to the problem in my code.
My program works apparently fine, but I'm not able to pass the check50 test. From what I understand, the issue may be related to the fact that the null \0 is printed. But I don't know how to modify that. Could you please help me?

This is my code:

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

bool only_digits(string s);
char rotate(char c, int n);
int main(int argc, string argv[])
{
string h = argv[1];
if (argc != 2 || !only_digits(h) || h <= 0)
    {printf("Usage: ./caesar key\n");
    return 1;
    }
else
{
    int key = atoi(argv[1]);

string plaintext = get_string("plaintext:  ");

    int f = strlen(plaintext);
    printf("ciphertext: ");
    for(int q = 0; q < f; q++)
    {
        printf("%c", rotate(plaintext[q], key));
    }
    printf("\n");
}
    return 0;
}


bool only_digits(string s )
{
    for (int i = 0, n = strlen(s); i < n; i++)
{
    char digit = s[i];
    if (!isdigit(digit))
    return false;
}
return true;
}

char rotate(char c, int n)
{
    if(isupper(c) && (c != '\0'))
{
    printf("%c", (((c - 65) + n) % 26) + 65);
}
    else
    if(islower(c) && (c != '\0'))
    {
        printf("%c", (((c - 97) + n) % 26) + 97);
    }
    else
    printf("%c", c);
return 0;
}

This is a caption of check50's check:
check50

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

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

发布评论

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

评论(1

慈悲佛祖 2025-01-19 22:50:42

rotate 始终返回 0,因此 printf("%c",rotate(plaintext[q], key)); 会导致您输入字母输出以 NUL 字符间隔。

我会保留 printf,但更改 rotate 以返回字符而不是打印它。

rotate always returns 0, so printf("%c", rotate(plaintext[q], key)); is causing the letters you output to be interspaced with NUL characters.

I would keep that printf, but change rotate to return the character instead of printing it.

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