[英]How to properly convert an array string of 'numbers', using a cipher of letters?
我將進入正題。 我知道這應該很簡單,但給了我解決方法。 我正在一個安全的本地網絡上工作。 我需要輸入一個輸入(在我的例子中是一個 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 = "DKSLEIANQP";
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 **’```
您代碼中的主要問題是 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;
}
在這一行
void cwitch(char *instr[MAXLEN])
你是說cwitch
接受一個字符指針數組(長度為MAXLEN
)。 它應該只是char *instr
。 類似的,在這一行
char *output[count];
您正在聲明一個char *
數組,而您希望它是一個char
數組: char output[count]
。 最后,您不能將字符'x'
與字符串進行比較
if (instr[i] == ".")
-- 它必須又是一個字符文字 ( '.'
)。
一個小邏輯問題:如果輸入字符是.
您不能“什么都不做”,還必須將其復制到輸出字符串中。
通過這些修復,您的程序實際上可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.