我正在尝试接收一串字符并将它们递归地分成两半,直到字符长度仅为2.然后取char字符串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
我不是要求任何人为我做任务。只是想在正确的方向上轻推一下,因为我正在艰难地绕着如何连续分开琴弦然后交换。
答案 0 :(得分:1)
OP: "I'm not asking anyone to do my assignments for me. Just looking for a nudge in the right direction"
The recursive code needs to split the string and be careful about the length, be it odd or even.
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, ___);
}