奇怪的字符出现在输出中

Strange char appears in the output

我正在尝试混淆存储在字符串中的单词,我的代码有时有效,有时无效。这是我的代码:

// main function
int main(int argc, string argv[])
{
    string k, plaintext;
    int size, i = 0, key = 0;
    k = argv[1];
    size = strlen(k);
    if (argc < 2 || !isNummeric(k, size) || k < 0)
        return 1;
    else
    plaintext = GetString();
    size = strlen(plaintext);
    char ciphertext[size];
    key = atoi(k);
    while(i < size)
    {
        if (isalpha(plaintext[i]))
        {
            encipher(key, i, &ciphertext[i], plaintext);
        }
        else
        {
            ciphertext[i] = plaintext[i];
        }
        i++;
    }
    printf("%s\n",ciphertext);
}

从用户那里收到一个键来移动每个字母,我需要检查这个键是否是数值所以我制作了 isNummeric 函数来做到这一点

bool isNummeric(string k, int size)
{
    int c=0;
    for(int i=0; i<size; i++)
    {
        if(!isdigit(k[i]))
        c++;
    }
    if(c==0)
    return true;
    return false;
}

现在为了加密我做了一个函数来移动每个字母:

void encipher(int k, int i, char *pt, string plaintext)
{
    int p, c;
    if(islower(plaintext[i]))
    {
        p=plaintext[i]-'a';
        c=(p+k)%26;
        *pt=c+97;
    }
    else
    {
        p=plaintext[i]-'A';
        c=(p+k)%26;
        *pt=c+65;
    }
}

将问题中的多个评论组合成固定代码会产生以下似乎有效的代码:

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

static bool isNumeric(string k, int size)
{
    for (int i = 0; i < size; i++)
    {
        if (!isdigit((unsigned char)k[i]))
            return false;
    }
    return true;
}

/* There are too many arguments to this function - but it works */
static void encipher(int k, int i, char *pt, string plaintext)
{
    int p, c;
    if (islower((unsigned char)plaintext[i]))
    {
        p = plaintext[i] - 'a';
        c = (p + k) % 26;
        *pt = c + 'a';
    }
    else
    {
        p = plaintext[i] - 'A';
        c = (p + k) % 26;
        *pt = c + 'A';
    }
}

int main(int argc, string argv[])
{
    string k = argv[1];
    if (argc < 2 || !isNumeric(k, strlen(k)))
        return 1;
    string plaintext = GetString();
    int size = strlen(plaintext);
    char ciphertext[size + 1];
    int key = atoi(k);
    for (int i = 0; i < size; i++)
    {
        if (isalpha(plaintext[i]))
        {
            encipher(key, i, &ciphertext[i], plaintext);
        }
        else
        {
            ciphertext[i] = plaintext[i];
        }
    }
    ciphertext[size] = '[=10=]';
    printf("%s\n", ciphertext);
}

程序被调用 csr13,并给出以下输出:

$ csr13 4
The Quick Brown Fox Jumped Over The Lazy Dog
Xli Uymgo Fvsar Jsb Nyqtih Sziv Xli Pedc Hsk
$ csr13 22
Xli Uymgo Fvsar Jsb Nyqtih Sziv Xli Pedc Hsk
The Quick Brown Fox Jumped Over The Lazy Dog
$

encipher 函数的更好设计是传递单个字符加上 'key' 偏移量,并将 return 加密字符:

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

static bool isNumeric(string k, int size)
{
    for (int i = 0; i < size; i++)
    {
        if (!isdigit((unsigned char)k[i]))
            return false;
    }
    return true;
}

static int encipher(int k, int c)
{
    assert(isalpha(c));
    if (islower(c))
        return (c - 'a' + k) % 26 + 'a';
    else
        return (c - 'A' + k) % 26 + 'A';
}

int main(int argc, string argv[])
{
    string k = argv[1];
    if (argc < 2 || !isNumeric(k, strlen(k)))
        return 1;
    string plaintext = GetString();
    int size = strlen(plaintext);
    char ciphertext[size + 1];
    int key = atoi(k);
    for (int i = 0; i < size; i++)
    {
        if (isalpha(plaintext[i]))
            ciphertext[i] = encipher(key, plaintext[i]);
        else
            ciphertext[i] = plaintext[i];
    }
    ciphertext[size] = '[=12=]';
    printf("%s\n", ciphertext);
}