c - 递归换位密码C

标签 c recursion encryption

我正在尝试获取一串字符并将它们递归地分成两半,直到字符长度仅为 2。然后获取 2 的字符字符串并将其与它旁边的字符字符串交换。我的代码如下:

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

void encrypt(char *str, int size);

int main(){
    char input[8192];
    int length;

    printf("INPUT A PHRASE: ");
    fgets(input, 8192, stdin);
    length = strlen(input) -1;
    printf("LENGTH: %d\n", length);

    encrypt(input, length);
    printf("ENCRYPTION: %s\n", input);  
    return 0;
}

void encrypt(char str[], int size){
    int i;
    int k = size / 2;

    //Encryption code here

}

一些示例输出:

Sample Input:
12345678

Sample Output:
34127856

Sample Input:
Test early and often!

Sample Output:
aeyrleT sttf!enn aod

我不会要求任何人为我完成作业。只是在寻找正确方向的插入力,因为我很难思考如何连续拆分字符串然后交换。

最佳答案

OP: "I'm not asking anyone to do my assignments for me. Just looking for a nudge in the right direction"

递归代码需要拆分字符串并注意长度,无论是奇数还是偶数。

void encrypt(char str[], int size){

  if (size <= 2) {
    // do encryption, TBD code
    retrun;
  }

  int i;
  int k = size / 2;  //  good step 1

  char *left = ____; // what is the address of the left half?  (easy)

  // call encrypt of the left half
  encrypt(left, ___); // what is the size of the left half? (easy)

  // Now do right
  char *right = ____; // what is the address of the R half? 
                      // (hint, it begins at the left half end.)

  // What is the R half length (hint: total - ???)
  // now call encrypt again with R half and length
  encrypt(right, ___);

}

关于c - 递归换位密码C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35165903/

相关文章:

java - 递归打印句子中的单词

java - 保存到文件时无法使用 AES 256 算法解密字符串

python - 如何使用python 3解密SQLite数据库中的项目

c - 无符号整数的for循环

c++ - 为什么 C++ 标准库与编译器而不是操作系统捆绑在一起?

c - 易变的变量

python - 如何正确模仿这种加密方法来为 encryptedPwd 字段生成正确的值?

c - C语言中如何判断一个字符串是否为有效日期

带有列表的 Python 回合制递归

python - python中的递归搜索