#include <stdio.h>

void caesar (char cipher[], int shift);

int main () {

char cipher[50];
int shift;

  printf("Enter text to be encrypted IN CAPITAL LETTERS ONLY: ");
  scanf("%s", cipher);

  printf("How many shifts do you prefer? 1-10 only: ");
  scanf("%d", &shift);

  caesar (cipher, shift);

  return 0;
}

void caesar (char cipher[], int shift) {
  int i = 0;

  while (cipher[i] != '\0') {
    if ((cipher[i] += shift) >= 65 && (cipher[i] += shift) <= 90) {
      cipher[i] += (shift);
    } else {
      cipher[i] += (shift - 25);
    }
    i++;
  }
  printf("%s", cipher);
}

我开始获得加密的输出,但是我的声明恐怕有问题。

例如:
  • 输入:ABCD,一类制
  • 输出:DEFG

    最佳答案

    改变

    if ((cipher[i] += shift) >= 65 && (cipher[i] += shift) <= 90) ...
    


    if ((cipher[i] + shift) >= 65 && (cipher[i] + shift) <= 90) ...
    

    因为+=修改了cipher[i]

    关于c - 凯撒的密码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8464936/

  • 10-11 23:51