【问题标题】:Need help in Caesar Cipher in C在 C 中需要凯撒密码的帮助
【发布时间】:2015-08-03 08:50:24
【问题描述】:

我是编程新手,我正在尝试用 C 语言编写一个用于凯撒密码的程序。

输入由一个等于字符串长度的整数 ilength,后跟字符串 str 和一个整数 encrypt。

我的输入是:

11
middle-Outz
2

输出:

okffng-Qwv@

需要的输出是:

okffng-Qwvb

下面是我写的代码。有人可以帮助我为什么我在输出中弄错了最后一个字符!

我一无所知。

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

int main()
{
  int ilength = 0, encrypt = 0, i = 0, j = 0;

  char alph_base[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

  scanf("%d", &ilength);
  char str[ilength + 1];
  scanf("%s", str);
  scanf("%d", &encrypt);
  //printf("%c\n", str[5]);

  char outputString[ilength + 1];
  char temp[ilength + 1];

  for (j = 0; j <= ilength; j++)
  {
    temp[j] = str[j];
    i = 0;

    if (str[j] == '\0')
    {
      outputString[j] = '\0';
    }

    while ((i >= 0) && (i < 26))
    {
      if (temp[j] == alph_base[i])
      {
        if (i == 25 && encrypt == 0)
        {
          outputString[j] = alph_base[25];
        }

        if ((i + encrypt) == 26)
        {
          outputString[j] = alph_base[(i + encrypt) % 26];
        }
        else
          outputString[j] = alph_base[(i + encrypt) % 26];
      }
      if ((temp[j] < 65 || temp[j] > 90) && temp[j] < 97)
        outputString[j] = temp[j];

      if ((temp[j] < 97 || temp[j] > 122) && temp[j] > 90)
        outputString[j] = temp[j];

      i++;
    }

    while ((i > 25) && (i < 52))
    {
      if (temp[j] == alph_base[i])
      {
        if (i == 51 && encrypt == 0)
        {
          outputString[j] = alph_base[51];
        }

        if ((i + encrypt) == 51)
        {
          outputString[j] = alph_base[51];
        }

        if ((i + encrypt) > 51)
        {
          outputString[j] = alph_base[((i + encrypt) % 51) + 25];
        }
        else
          outputString[j] = alph_base[(i + encrypt) % 51];
      }
      if ((temp[j] < 65 || temp[j] > 90) && temp[j] < 97)
        outputString[j] = temp[j];

      if ((temp[j] < 97 || temp[j] > 122) && temp[j] > 90)
        outputString[j] = temp[j];
      i++;
    }
  }
  printf("%s\n", outputString);
  return 0;
}

【问题讨论】:

  • 在条件块if (str[j] == '\0') { ... } 之后,您可能需要用else { ... } 包装循环体的其余部分。
  • @Giorgi :我对编码很陌生。会尽我所能改进。谢谢
  • @squeamishossifrage :好的,会检查的。谢谢
  • 建议的答案非常好。它涵盖了所有情况并有助于减少您的代码。你应该使用那个
  • @JohnDemetriou:当然。谢谢

标签: c caesar-cipher


【解决方案1】:

您的代码对于您想做的事情来说太复杂了。

您的问题似乎与从'z' 循环回'a' 有关。

像这样的简单函数可以为角色完成工作:

#include <ctype.h>

char caesar_encrypt(char input, int key)
{
    char output = input;
    char base, offset;
    // If not a letter, return the char unmodified
    if (! isalpha(input))
    {
        return output;
    }

    base = isupper(input) ? 'A' : 'a'; // Check if upper/lower case
    offset = input - base; // Take offset from 'a'
    offset += key; // Add key to offset
    offset %= 26; // Wrap offset to the 26 letters

    output = base + offset;
    return output;
}

这里有很多想法:

  1. 使用来自&lt;ctype.h&gt;(isalpha、isupper)的函数,这样可以避免代码中的许多比较。

  2. 将您的字符视为与字母 A(大写或小写 A)的“偏移”。因此,您正在处理[0;25] 范围内的数字,并且可以使用简单的模数进行包装

  3. 字符是“整数”,因此您可以添加或减去它们。要获取大写字母的第三个字母,您可以使用char c = 'A' + 2;,这比您的庞大数组更简单。

免责声明:此处编写的代码未经测试,可能包含拼写错误;)

【讨论】:

  • 非常感谢。我不知道 中的那些有用功能
  • 工作就像一个魅力!谢谢。关键的学习是我可以使用 函数并且可以添加/减去字符。
【解决方案2】:

NiBZ 的完美回答。此外,如果您仍然想编写自己的检查而不是使用库函数,您可以执行以下操作:

int my_isalpha(char c){
    return ((c >= 'a' && c <= 'z')|| (c >= 'A' && c <= 'Z'));
}

int my_isupper(char c){
    return (c >= 'A' && c <= 'Z');
}

警告:上面的实现对于 ASCII 很好,但对于 ISO 8859-1 或其亲属来说不是很好。参考:https://stackoverflow.com/a/2169293/5183246

【讨论】:

  • 谢谢@d_geeks。我也要试试这个
猜你喜欢
  • 2023-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多