奇怪的字符出现在输出中

时间:2016-07-29 16:58:50

标签: c cs50 caesar-cipher

我试图混淆存储在字符串中的单词,我的代码有时会起作用,有时也不会。这是我的代码:

// 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;
    }
}

here is my output

1 个答案:

答案 0 :(得分:0)

将问题中的多个注释汇总到固定代码中会产生以下代码:

#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] = '\0';
    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'偏移量并返回加密字符:

#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] = '\0';
    printf("%s\n", ciphertext);
}