Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C索引字符数组中编写基本字符串加密程序时遇到问题_C_Pointers_Char - Fatal编程技术网

在C索引字符数组中编写基本字符串加密程序时遇到问题

在C索引字符数组中编写基本字符串加密程序时遇到问题,c,pointers,char,C,Pointers,Char,我在尝试编写一个用C语言“加密”字符串的程序时遇到了一点困难。该程序的目标是允许用户通过输入一个密码来加密字符串,该密码将用于交换原始字符串中的字母,使其混乱。我原来的程序比这复杂得多,有逻辑来处理解析用户输入密码和维护case(这一切都很有效,令人震惊!)。但是,我无法解决的绝对基本问题是,我的“encryption”函数返回的字符串不正确 我已经重新编写了另一个赤裸裸的函数版本来尝试和调试它;在我开始查找信息之前,这是我天真的程序版本: // Barebones character swap

我在尝试编写一个用C语言“加密”字符串的程序时遇到了一点困难。该程序的目标是允许用户通过输入一个密码来加密字符串,该密码将用于交换原始字符串中的字母,使其混乱。我原来的程序比这复杂得多,有逻辑来处理解析用户输入密码和维护case(这一切都很有效,令人震惊!)。但是,我无法解决的绝对基本问题是,我的“encryption”函数返回的字符串不正确

我已经重新编写了另一个赤裸裸的函数版本来尝试和调试它;在我开始查找信息之前,这是我天真的程序版本:

// Barebones character swap for the encryption program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define GET_CHARS_MAX 100

const char plainAlphabet[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 prebakedCypher[26] = {'v', 'c', 'h', 'p', 'r', 'z', 'g', 'j', 'n', 't', 'l', 's', 'k',
                                 'f', 'b', 'd', 'q', 'w', 'a', 'x', 'e', 'u', 'y', 'm', 'o', 'i'};

int encryptString(char *encryptStr, int stringLength)
{
    for (int i = 0; i < 26; i++)    // For each letter in the alphabet
    {
        for (int j = 0; j < stringLength; j++)    
        {
            if (encryptStr[j] == plainAlphabet[i]) // Find and replace that letter on each pass
            {
                encryptStr[j] = prebakedCypher[i];
            }
        }
    }
}

int main()
{
    char *userString      = malloc(GET_CHARS_MAX);
    char *stringToEncrypt = malloc(GET_CHARS_MAX); 
    // Get user input into string
    printf("\n Enter a string, 100 characters long maximum: ");
        fgets(userString, GET_CHARS_MAX, stdin);
        int stringLength = strlen(userString) - 1;    // Remove null I think?

    // Make a copy of the input to encrypt
    strcpy(stringToEncrypt, userString);
        printf("\n Copied string: %s", stringToEncrypt);

    // Check that indexing the alphabet and cypher actually works
    // the way that you think it does
    printf("\n Alphabet element 1: %c \n Cypher element 1: %c", plainAlphabet[0], prebakedCypher[0]);
    printf("\n Alphabet element 't': %c \n Cypher element 't': %c", plainAlphabet[19], prebakedCypher[19]);

    // 'Encrypt' string and display result
    printf("\n Plaintext string: %s", userString);
        encryptString(stringToEncrypt, stringLength);
    printf("\n Encrypted string: %s", stringToEncrypt);

    return 0;
}
我喜欢用
printf
尽可能多地检查;字符数组似乎索引正确,但在循环的
中分配不正确(t'和'h'都是'm',a'被分配字符'u',依此类推)。
我查阅了如何正确索引字符数组,并找到一些帖子,指导我创建一个指向数组开头的额外指针,然后使用循环计数器执行指针算术,以区分所需元素上的指针。我可怕的尝试是:

int encryptString(char *encryptStr, int stringLength)
{
    char *cypherPointer = &prebakedCypher[0];

    for (int i = 0; i < 26; i++)    // For each letter in the alphabet
    {
        for (int j = 0; j < stringLength; j++)    
        {
            if (encryptStr[j] == plainAlphabet[i]) // Find and replace that letter on each pass
            {
                encryptStr[j] = (*cypherPointer) + i;
            }
        }
    }
}

密码中没有重复字母,因此“b”和“c”都不能替换为“m”。我们还知道“a”应该替换为“v”,而不是“u”。

如果要替换字符串,请检查每个字符26次,根据密码和起始字符的不同,在1到26次之间进行替换

例如,这就是为什么在您的示例中“b”和“c”都变成“m”的原因:

  • “b”替换为“c”
  • 在下一次迭代中,将“c”替换为“h”
  • 5次迭代后,“h”替换为“j”
  • 2次迭代后,“j”替换为“t”
  • 10次迭代之后,“t”被替换为“x”
  • 4次迭代之后,“x”最后一次替换为“m”
相反,只需检查每个字符最多26次,并准确替换一次:

int encryptString(char *encryptStr, int stringLength)
{
    for (int i = 0; i < stringLength; i++)    // For each letter in input
    {
        for (int j = 0; j < 26; j++)    
        {
            if (encryptStr[i] == plainAlphabet[j]) // Find and replace that letter 
            {
                encryptStr[i] = prebakedCypher[j];
                break;
            }
        }
    }
}
int-encryptString(char*encryptStr,int-stringLength)
{
for(int i=0;i
啊,当然。我的循环比我想象的做得更好!我考虑过按照你一开始概述的方式来做,但我认为这需要不必要的过程,因为你(可能)要为每个字母搜索整个字母表。我试图在每次通行证上替换出现的每一封信。先让它工作,然后再优化@罗文:当然可以,但你总是要为每个字母搜索整个字母表。当然,你能做的最好的事情就是找到匹配的字母后立即转到下一个字母。再次感谢您立即发现我的愚蠢问题,并将其添加到您的故障中!我将来会留意类似的东西。
 Enter a string, 100 characters long maximum: abcdef

 Copied string: abcdef

 Alphabet element 1: a
 Cypher element 1: v
 Alphabet element 't': t
 Cypher element 't': x
 Plaintext string: abcdef

 Encrypted string: ummdoi
int encryptString(char *encryptStr, int stringLength)
{
    for (int i = 0; i < stringLength; i++)    // For each letter in input
    {
        for (int j = 0; j < 26; j++)    
        {
            if (encryptStr[i] == plainAlphabet[j]) // Find and replace that letter 
            {
                encryptStr[i] = prebakedCypher[j];
                break;
            }
        }
    }
}