CS50凯撒:分割故障问题

发布于 2025-01-24 17:43:23 字数 1119 浏览 3 评论 0原文

我目前正在参加CS50课程,并且问题集2(Caesar)遇到了问题。我尝试运行下面的代码,但它给了我**分段错误**警告,但我似乎无法发现我自己的问题。 请问有什么问题,我该如何解决?

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

bool only_digits(string s);
int main(int argc, string argv[])
{
    int i,n;
    string text;
    n = strlen(text);
    string s = argv[1];
    int k = atoi(argv[1]);

    if (argc != 2 || !only_digits(s))
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }

    text = get_string("plaintext: ");

         for(i=0; i<n; i++)
    {
        if(isupper(text[i]))
        {
            char r, z;
             r = text[i] + k;
             while (r>90)
             {
                  z = r-90;
                  text[i]= 64 + z;

             }
        }

        if(!isalpha(text[i]))
        {
            text[i]= text[i]+0;
        }
    }


}


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

I'm currently taking the cs50 course and I'm having an issue with the problem set 2 (caesar).I tried running the code below but it gave me a **SEGMENTATION FAULT ** warning but I can't seem to spot the problem by myself.
Please what is the problem and how do I resolve it?

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

bool only_digits(string s);
int main(int argc, string argv[])
{
    int i,n;
    string text;
    n = strlen(text);
    string s = argv[1];
    int k = atoi(argv[1]);

    if (argc != 2 || !only_digits(s))
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }

    text = get_string("plaintext: ");

         for(i=0; i<n; i++)
    {
        if(isupper(text[i]))
        {
            char r, z;
             r = text[i] + k;
             while (r>90)
             {
                  z = r-90;
                  text[i]= 64 + z;

             }
        }

        if(!isalpha(text[i]))
        {
            text[i]= text[i]+0;
        }
    }


}


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

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

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

发布评论

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