凯撒密码打印出数字而不是解密文本?在 C

Caesar cipher printing out numbers instead of decrypted Text? in C

所以我有这个凯撒密码程序,但是当我 运行 它只打印出数字而不是解密的文本。有人知道我错过了什么吗?我相信 bool solved 函数可能有问题。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "rotUtils.h"

bool solved( char decodearr[], char dictarr[][30], int size1, int size2){

    char* compared;
    bool result = false;

    for(int j = 0; j < size2; j++){

    compared = strstr( decodearr, dictarr[j]);

    }
    if( compared != '[=10=]'){

     result = true;

         }
    return result;

}

int decode( char codearr[], char dictarr[][30], int size1, int size2)
    {
    bool solution = false;
    int key = -50;
    char decodearr[10000];
    while(solution == false && key < 51)
    {
     for( int i = 0; i < size1; i++)
        {
        if(!isspace(codearr[i]))
         {
          decodearr[i] = rotate(codearr[i], key);
          }
        else
          decodearr[i] = codearr[i];


    }

    solution = solved( decodearr, dictarr, size1, size2);
    if( solution == false)
     {
      key++;
      }
    }
  for( int j = 0; j < size1; j++)
    {
    codearr[j] = decodearr[j];
    }
  return key;
}

    int main( int argc, char* argv[])
    {
    char* file = argv[1];
    char* dictionary = argv[2];
    char code[10000];
    char dict[30000][30];
    FILE* codeFile;
    codeFile = fopen(file, "r");
    int i = 0;
    int j = 0;
    int key;
    FILE* dictFile;
    dictFile = fopen(dictionary, "r");

    while(!feof(codeFile))
    {
    code[i] = fgetc(codeFile);
    i++;
    }

    code[ i + 1] = '[=10=]';
    fclose(codeFile);

    while(!feof(dictFile))
    {
    fscanf(dictFile, "%s", dict[j]);
    j++;
    }

    key = decode(code, dict, i, j);
    fclose(dictFile);

        for(int k = 0; k < i; k++)
        {
        printf("%d", code[k]);
        }

    printf( "\nThe key is: %d\n", key);

    return 0;
}

printf("%d", code[k]); 表示 "print out the decimal digits that represent the integer code[k]".

如果你想“打印出代表整数 code[k] 的字符,那么你需要 %c 格式说明符:printf("%c", code[k]);

你只打印数字

 printf("%d", code[k]);

也许试试

printf("%c", code[k]);

打印数字代表的字符。

当您想要打印 code[k] 时,只需在您的代码中使用 "%c" 而不是 "%d"。 祝你好运!