【问题标题】:CS50 CAESAR PSET2 Problems to cipher text via key from command-line argument argv[]CS50 CAESAR PSET2 通过命令行参数 argv[] 的密钥加密文本的问题
【发布时间】:2020-05-12 23:37:09
【问题描述】:

所以我需要编写一个加密文本的程序,将 x (argv[1] = x) 添加到提示给用户的文本中。即:

./program 1  //running the program with argv[1] = 1

plaintext: abcd  //prompting the user to write characters he want to cipher
ciphertext: bcde  // returning plaintext ciphered with the argv[1] "key" = 1

这是我的代码

int main (int argc, string argv[])
{   
    if (argc != 2)
    {
        printf("Usage: ./ceasar key\n");
        return 1;
    }
    else if (argc ==2)
        {
            int k = atoi(argv[1]);
            for (int j = 0, len = strlen(argv[1]); j < len; j++)
            {
                if (!isdigit(argv[1][j]))
                {
                    printf("Usage: ./ceasar key\n");
                    return 1;
                }

            }
            for (int j = 0, len = strlen(argv[1]); j < len; j++)
            {
                 if (isdigit(argv[1][j]))
                {
                    string s = get_string("plaintext: ");
                    printf("ciphertext: ");
                    for (int i = 0, n = strlen(s); i <= n; i++)
                    {

                        if ('@' < s[i] && s[i] < '[')
                        {
                            printf("%c", (s[i] - 'A' + k) % 26 + 'A');
                        }
                        else if('`' < s[i] && s[i] < '{')
                        {
                            printf("%c", (s[i] - 'a' + k) % 26 + 'a');
                        }
                        else
                        {
                            printf("%c", s[i]);
                        }
                        printf("\n");
                        return 0;
                    }  
                }
            }
        }
    }```


The first lines checks if argc !=2, and if argv[1][j] has a non numeric character. Once that is done it will get argv[1] and add it to each character given from the user. but it wont work correctly. 

**Any sugestions?**


【问题讨论】:

  • 2b 为关键字尝试您的程序,看看您是否可以确定问题所在。或许阅读man atoi 以获得更多“提示”。
  • 这能回答你的问题吗? Got stuck with Caesar.c
  • OT:关于:printf("Usage: ./ceasar key\n"); 错误消息应该输出到stderr,而不是stdout。建议:fprintf( stderr, "Usage: %s key\n". argv[0] );
  • OT: about: for (int j = 0, len = strlen(argv[1]); j &lt; len; j++) 这将导致编译器输出关于将unsigned 值与signed 值进行比较的警告。注意:函数:strlen() 返回一个 size_t (unsigned long int),它正在与变量 j 中的 int 值进行比较
  • This may help you实现您的预​​期输出。输入key = 1 和你的message = "abcd"

标签: c cs50 caesar-cipher


【解决方案1】:

我测试了您的代码。它打印一个字符,这意味着您的最终 for loop 返回太快。这个循环只迭代一次,因为它在迭代结束时返回0。如果您将return 0printf("\n") 一起移出for 循环,您的代码将起作用。

解决方案;

for (int j = 0, len = strlen(argv[1]); j < len; j++)
{
    if (isdigit(argv[1][j]))
    {
        string s = get_string("plaintext: ");
        printf("ciphertext: ");
        for (int i = 0, n = strlen(s); i <= n; i++)
        {

            if ('@' < s[i] && s[i] < '[')
            {
                printf("%c", (s[i] - 'A' + k) % 26 + 'A');
            }
            else if('`' < s[i] && s[i] < '{')
            {
                printf("%c", (s[i] - 'a' + k) % 26 + 'a');
            }
            else
            {
                printf("%c", s[i]);
            }
        }
        printf("\n");
        return 0;
    }
}

【讨论】:

  • 非常感谢!我在阅读本文之前就这样做了,它有效!但是我再次来到这里,因为当我检查程序是否使用 check50 运行良好时,我发现它没有按预期加密。但确实如此。真的很奇怪。
  • 欢迎您。如果我回答了您的问题,请将我的帖子标记为“作为答案”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-08
  • 2016-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-17
  • 2020-04-02
相关资源
最近更新 更多