code

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

bool only_digits(string s);
char rotate(char c, int n);

int main(int argc, string argv[])
{
    if (argc < 2 || only_digits(argv[1]) == false || argc > 2) // check for th correct input
    {
        printf("usage: ./caesasr key \n");
    }
    else
    {
        int key = atoi(argv[1]);
        string plaintext = get_string("plaintext: ");
        int x = strlen(plaintext);
        char cyphertext[x + 1];
        for (int i = 0; i < x; i++)
        {
            cyphertext[i] = rotate(plaintext[i], key);
        }
        printf("cyphertext: %s\n", cyphertext);
    }
}

//make a function bool only_digits to check for input is a single digit between 0-9

bool only_digits(string s)
{
    if (s[0] > 47 && s[0] < 58 && strlen(s) == 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//make a function char rotate(char c, int n) that rotate c by n on the alpahbet
// cout = (cin -65 + n)%26 +65 (uppercase)
// cout = (cin -97 + n)%26 +97 (lowercase)
// cout = cin (other)

char rotate(char c, int n)
{
    if (c > 64 && c < 91)
    {
        c = ((c - 65 + n) % 26 + 65);
    }
    if (c > 96 && c < 123)
    {
        c = ((c - 97 + n) % 26 + 97);
    }
    else
    {
        return c;
    }
    return c;
}

CLI outputs, question marks and chars added randomly to the cyphertext

我不知道问号和随机字符是从哪里来的; 它似乎只适用于5个字母的输入; DEBUG命令会发现一切工作正常,直到打印出最后一封输出信,然后突然添加了随机字符.

编辑:将第21行更改为 char cyphertext[x];个 没有解决问题

推荐答案

输出字符串不是以空结尾的:您在末尾为空字节添加了空格,但没有显式设置它.数组cyphertext未初始化,因此cyphertext[i]处的字符可以是任何字符,并且printf输出它,然后在数组之后继续读取和打印存在于存储器中的字符,这是未定义的行为.

您应该在for循环之后设置空终止符

            cyphertext[x] = '\0';

还要注意以下问题:

  • 为了可读性,你应该使用字符常量,比如'A',而不是像65这样的显式ASCII代码.
  • only_digits应测试所有字符,并接受长度超过1个字节的字符串作为最多25个密钥值.

以下是修改后的版本:

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

bool only_digits(string s);
char rotate(char c, int n);

int main(int argc, string argv[])
{
    if (argc != 2 || only_digits(argv[1]) == false) // check for th correct input
    {
        printf("usage: ./caesasr key \n");
        return 1;
    }
    else
    {
        int key = atoi(argv[1]);
        string plaintext = get_string("plaintext: ");
        int x = strlen(plaintext);
        char cyphertext[x + 1];
        for (int i = 0; i < x; i++)
        {
            cyphertext[i] = rotate(plaintext[i], key);
        }
        cyphertext[x] = '\0';
        printf("cyphertext: %s\n", cyphertext);
        return 0;
    }
}

//make a function bool only_digits to check for input is a string with one or two digits

bool only_digits(string s)
{
    int len = strlen(s);
    if (len < 1 || len > 2)
    {
        return false;
    }
    for (int i = 0; i < len; i++)
    {
        if (s[i] < '0' || s[i] > '9')
        {
            return false;
        }
    }
    return true;
}

//make a function char rotate(char c, int n) that rotate c by n on the alpahbet
// cout = (cin - 'A' + n) % 26 + 'A' (uppercase)
// cout = (cin - 'a' + n) % 26 + 'a' (lowercase)
// cout = cin (other)

char rotate(char c, int n)
{
    if (c >= 'A' && c <= 'Z')
    {
        return (c - 'A' + n) % 26 + 'A';
    }
    else if (c >= 'a' && c <= 'z')
    {
        return (c - 'a' + n) % 26 + 'a';
    }
    else
    {
        return c;
    }
}

C++相关问答推荐

如何用C(使用两个s补数算术的32位程序)计算

找出文件是否包含给定的文件签名

不同到达时间的轮询实现

使用NameSurname扫描到两个单独的字符串

为什么GCC可以调用未定义的函数?

将fget()与strcMP()一起使用不是正确的比较

是什么让numpy.sum比优化的(自动矢量化的)C循环更快?

关于";*(++p)->;t";、&++p->;t";和&q;++*p->;t";的问题

为什么用非常数指针变量改变常量静态变量时会出现分段错误?

C语言中神秘的(我认为)缓冲区溢出

从不兼容的指针类型返回&&警告,但我看不出原因

静态初始化顺序失败是否适用于C语言?

int * 指向int的哪个字节?

程序对大输入给出错误答案

我编写这段代码是为了判断一个数字是质数、阿姆斯特朗还是完全数,但由于某种原因,当我使用大数时,它不会打印出来

可以';t从A9G模块拨打电话

使用 strtok 多次分割一个字符串会导致意外行为

10 个字节对于这个 C 程序返回后跳行的能力有什么意义

SSE 向量与 Epsilon 的比较

如何修复数组数据与列标题未对齐的问题?