c++ - Caesar Cipher C++(使用字符指针和移位作为参数)

标签 c++ pointers char encryption

我正在寻找这样的方法(使用凯撒密码加密消息,由用户输入并显示):

void encrypt(char *message, int shift);

我的代码:

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

char num(char c)
{
    const char upper_alph[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    const char lower_alph[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

    if(isupper(c)) {
        for(int i = 0; i < 26; i++)
            if(upper_alph[i] == c)
                return i;
    } else {
        for(int i = 0; i < 26; i++)
            if(lower_alph[i] == c)
                return i;
    }
    return 0;
}

void encrypt(char *message, int shift)
{
    int i = 0;
    const char upper_alph[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    const char lower_alph[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    while(message[i] != NULL)
    {
        if(isalpha(message[i]))
        {
            if(isupper(message[i])) {
                printf("%c", upper_alph[(num(message[i])+shift)%26]);
            } else {
                printf("%c", lower_alph[(num(message[i])+shift)%26]);
            }
        } else {
            printf("%c", message[i]);
        }
            i++;
    }
}

#define OK       0
#define NO_INPUT 1
#define TOO_LONG 2

static int getLine (char *prmpt, char *buff, size_t sz) {
    int ch, extra;

    // Get line with buffer overrun protection.
    if (prmpt != NULL) {
        printf ("%s", prmpt);
        fflush (stdout);
    }
    if (fgets (buff, sz, stdin) == NULL)
        return NO_INPUT;

    // If it was too long, there'll be no newline. In that case, we flush
    // to end of line so that excess doesn't affect the next call.
    if (buff[strlen(buff)-1] != '\n') {
        extra = 0;
        while (((ch = getchar()) != '\n') && (ch != EOF))
            extra = 1;
        return (extra == 1) ? TOO_LONG : OK;
    }

    // Otherwise remove newline and give string back to caller.
    buff[strlen(buff)-1] = '\0';
    return OK;
}

int main()
{
    //reverse();
    //printf("\n\n");
    int rc;
    char mes[1024];
    int sh = 0;
    rc = getLine ("Enter message to be encrypted: ", mes, sizeof(mes));
    if (rc == NO_INPUT) {
        // Extra NL since my system doesn't output that on EOF.
        printf ("\nNo input\n");
        return 1;
    }
    if (rc == TOO_LONG) {
        printf ("Input too long [%s]\n", mes);
        return 1;
    }
    encrypt(mes, 1);
    fflush(stdin);
    getchar();
    return 0;
}

感谢任何提供帮助或试图提供帮助的人。

:)

编辑:做了很多更正。仍然无法正常工作:/

EDIT2:进行了更多更正。获取访问冲突 @"while(*message != '\0')"

EDIT3:将上面的代码更新为工作代码。谢谢大家的帮助!

最佳答案

一个问题是你永远不会环绕。考虑一下,如果您传递的是诸如“Z”或“z”之类的带有任何正向偏移的内容,那么您只会在数组之外递增。

你需要做类似的事情:

upper_alph[(num(message[i])+shift)%26]
    and
lower_alph[(num(message[i])+shift)%26]

你还需要为mes分配内存:

char mes[1024];

我相信你的 scanf 也是不正确的(c 是一个字符,s 是一个字符串):

scanf("%s", mes);

然而,使用 %s 只会读取直到出现空白,更好的选择可能是使用 getline() 读取整行.

关于c++ - Caesar Cipher C++(使用字符指针和移位作为参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15565577/

相关文章:

c++ - N Boost interval_set 的组合

c++ - 在 C++11 中对 vector 使用 for each

c - 在链表中添加节点时使用双指针的原因是什么?

字符-ASCII 关系

对 char 和 char * 感到困惑,并在单词中扫描

c++ - 为什么 & 对字符串的行为不同?

c++ - PlatformIO (Atom) 的 src 文件夹中是否可以有两个文件?

c++ - 按字母顺序对字符串数组排序的问题

c# - C# 中存储数组的位置

c - 双指针指向int变量覆盖内存