c - C中字符串的加减法(编码/解码)

标签 c

我正在做一个作业,我必须通过向将被编码/解码的字符串添加/减去“密码”(另一个字符串)来“编码”和“解码”一个字符串。但是,当我运行它时,我得到的结果与正确输出的结果相似,但仍然不正确。我认为这可能与我添加/减去两个字符串的方式有关。如果这是微不足道的,我很抱歉,但我是初学者。如果有任何帮助,我将不胜感激!

这是编码和解码的代码(注意:我还必须编写一个函数来查找字符串的长度,因为我不允许包含头文件)

int mystrlen(const unsigned char *string)
{
  int length = 0; /* holds the value of the length of the string */

   /* goes through elements of string not counting the null character */
  while(*string != '\0')
  {
    string++;  /* moves to the next letter of the string */
    length ++; /* counts how many letters there are      */
  }
  return length; /* returns the length of the string */
}

void jumble(unsigned char *string, const unsigned char *password, 
            enum CODE_METHOD method, int passes)
{
  if(method == ENCODE)
  {
    int i;
    for(i = 1; i <= passes; i++)
    {
      while(*string != '\0')
      {
        *string = *string + *password;

        string++;
        password++;
      }
    }
  }
  else
  {
    int i;
    for(i = 1; i <= passes; i++)
    {
      while(*string != '\0')
      {
        *string = (*string) - (*password);

        string++;
        password++;
      }
    }
  }
}

这是正确的输出:

Test0 ======================================
length of a is 1
length of b is 4
length of c is 0
length of d is 174

Test1 ======================================
Original phrase:
THIS IS A SECRET.

Encoded phrase:
xkstDl}AeC}fguouR

Test2 ======================================
Encoded phrase:
xkstDl}AeC}fguouR

Decoded back:
THIS IS A SECRET.

这就是我得到的:

Test0 ======================================
length of a is 1
length of b is 4
length of c is 0
length of d is 174

Test1 ======================================
Original phrase:
THIS IS A SECRET.

Encoded phrase:
xkst ┴╛ô╡@╓àh1

Test2 ======================================
Encoded phrase:
xkstDl}AeC}fguouR

Decoded back:
THISD5° e1ocp!

再次感谢您!

最佳答案

由于您的文本字符串比您的密码字符串,您正在运行超过密码字符串的末尾(例如,文本是 8 个字符,但您只有 4 个密码字符)。这是未定义的行为(例如之后的随机字符)

这是一些代码,当它到达结尾时回绕到密码的开头。我只是猜想这就是算法所需要的。您可能需要调整它以匹配您需要的确切算法/结果。

此外,string 变量必须在每次传递时重置为开头。否则,只有第一遍会产生效果。


void
jumble(unsigned char *string, const unsigned char *password, enum CODE_METHOD method, int passes)
{
    int pwlen;
    unsigned char *str;
    unsigned char *pw;

    pw = password;

    if (method == ENCODE) {
        int i;

        for (i = 1; i <= passes; i++) {
            //pw = password; ?
            str = string;
            while (*str != '\0') {
                *str = *str + *pw;
                str++;
                if (*++pw == 0)
                    pw = password;
            }
        }
    }
    else {
        int i;

        for (i = 1; i <= passes; i++) {
            //pw = password; ?
            str = string;
            while (*str != '\0') {
                *str = (*str) - *pw;
                str++;
                if (*++pw == 0)
                    pw = password;
            }
        }
    }
}

关于c - C中字符串的加减法(编码/解码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53075011/

相关文章:

c - 编译器是否将此逻辑操作作为函数处理?

c - 从 C 中的字符串中删除 .txt

为 ARM M4 编译和链接位置独立代码 (PIC)

将 char 转换为 unsigned int - C

c - 这个数组如何记录每个输入数字的出现?

c - 使用ctime获取2个操作之间的间隔?

c - 没有参数解释的 Printf

c++ - 在 C 中使用 SSL 的 mongoose 网络服务器的 hello world 示例

c - 信号问题,定时器(SIGEV_SIGNAL)

c - 如何允许使用 scanf 输入空格?