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

标签 c caesar-cipher

所以我有这个凯撒密码程序,但是当我运行它时它只打印出数字而不是解密的文本。有人知道我错过了什么吗?我相信 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 != '\0'){

     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] = '\0';
    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]); 表示“打印出代表整数 code[k] 的十进制数字”。

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

关于凯撒密码打印出数字而不是解密文本?在 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29338127/

相关文章:

c - 海湾合作委员会 : undefined reference to 'function'

c - 我无法向 char 数组添加偏移量,我从命令行接收字符串和整数的方法是最佳方法吗?

java - 尝试在 Java 中实现凯撒密码

c - 使用堆栈进行迭代合并排序

c - 在作用域的开头声明 C89 局部变量?

c++ - 编译 mpeg4ip 会出现解引用类型双关指针错误

C: 用户输入字符串包括 "' "-character

c - C 语言的简单凯撒密码程序

CS50 CAESAR - 根据语句位置不同的输出,但我不明白为什么

c++ - C/C++ 中的 '@' 和 '$' 是什么意思?