如何使用字母密码正确转换'数字'的数组字符串?

问题描述 投票:0回答:2

我会讲到重点。我知道这应该很简单,但可以解决。我正在安全的本地网络上工作。我需要输入(在我的情况下是IP地址)并加密该地址,然后安全地将其存储在数据库中。

我想将其加密为字母格式。很难解释,但是如果您查看此代码,则应该了解我正在尝试完成的工作。我已经对其进行了格式化,以接受用户(您)的输入,但是当此功能正常工作时,它将在后台实时调用。

理想情况下,用户将输入12.23.34.45

该功能将打印KS.SL.LE.EI

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

#define MAXLEN 16

void cwitch(char *instr[MAXLEN]){
    char *nstr = "0123456789";
    char *cstr = "DKSLEIANQK";

    int count = strlen(instr);
    char *output[count];

    int i;
    for(i = 0; i < count; i++){
        if(instr[i] != '\0'){
            int t;
            for(t = 0; t < count; t++){
                if(instr[i] == "."){ /*Do Nothing, Straight Logic - I Know This Is Useless*/ }
                else if(instr[i] == nstr[t]) output[i] = cstr[t];
            }
        }
    }
    output[count] = 0; //Null Terminate The String
    printf("Output: %s\n", output);
}

int main(void){
    char *input[500];
    printf("Enter Address: ");
    fgets(input, sizeof(input), stdin);
    input[strlen(input) - 1] = 0; //Remove \newline Character

    cwitch(input);
    return 0;
}

但是,执行后,我显然没有得到想要的结果,否则我就不会在这里!

[root@RyM ~]# ./test
Enter Address: 12.23.34.45
Output:
         `
[root@RyM ~]# ./test
Enter Address: 167.174.12.178
Output:
[root@RyM ~]# ./test
Enter Address: 45.223.6.1
Output:
         `
[root@RyM ~]# ./test
Enter Address: 918.6.56.222
Output:
[root@RyM ~]#

感谢您的帮助/建议。我是C语言的新手。在编译此代码时会出现警告。关于上述警告的存在,请随意烘烤/建议我。编辑:警告包括在下面

[root@RyM ~]# gcc -o test Test.c
Test.c: In function ‘cwitch’:
Test.c:10: warning: passing argument 1 of ‘strlen’ from incompatible pointer type
/usr/include/string.h:399: note: expected ‘const char *’ but argument is of type ‘char **’
Test.c:19: warning: comparison between pointer and integer
Test.c:19: warning: assignment makes pointer from integer without a cast
Test.c: In function ‘main’:
Test.c:31: warning: passing argument 1 of ‘fgets’ from incompatible pointer type
/usr/include/stdio.h:626: note: expected ‘char * __restrict__’ but argument is of type ‘char **’
Test.c:32: warning: passing argument 1 of ‘strlen’ from incompatible pointer type
/usr/include/string.h:399: note: expected ‘const char *’ but argument is of type ‘char **’```

c arrays string
2个回答
0
投票

代码中的主要问题是char数组(或const字符串)的定义错误。正确的定义是:

char output[count]; // where count is compile time constant

而不是char *output[count];

[]告诉编译器这是一个数组(它是指针的类型)。

因此,除非您要定义一个指针数组(不是您的情况),否则无需使用*

这里是一个清理过的代码:*注意:此代码远非完美,甚至还不错,我只是作了一些小小的改动以使其能够编译和工作。 *

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

#define MAXLEN 16

void cwitch(const char* instr) {
    const char* nstr = "0123456789";
    const char* cstr = "DKSLEIANQK";

    int count = strlen(instr);
    char output[MAXLEN];

    int i;
    for (i = 0; i < count; i++) {
        if (instr[i] != '\0') {
            int t;
            for (t = 0; t < count; t++) {
                if (instr[i] == '.') {
                    output[i] = '.';
                }
                else if (instr[i] == nstr[t]) output[i] = cstr[t];
            }
        }
    }
    output[count] = 0; //Null Terminate The String
    printf("Output: %s\n", output);
}

int main(void) {
    char input[500];
    printf("Enter Address: ");
    fgets(input, sizeof(input), stdin);
    input[strlen(input) - 1] = 0; //Remove \newline Character

    cwitch(input);
    return 0;
}

-1
投票

您需要了解*的含义:指向字符的指针

在此行

void cwitch(char *instr[MAXLEN])

您是说cwitch接受MAXLEN长度的字符指针数组。它应该只是char *instr。类似,在此行

char *output[count];

您正在声明char *的数组,而您希望它是char的数组:char output[count]。最后,您无法将字符'x'string

进行比较
if (instr[i] == ".")

-它必须再次是字符文字('.')。

一个小逻辑问题:如果输入字符为.,则不能“不执行任何操作”,也必须将其复制到输出字符串。

有了这些修复,您的程序就可以正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.