I need to encrypt the algorithm as follows
Algortihm :
Initially encoded string will be empty. Given an String, Each time the middle char of string will be picked, and appened to encoded string.
Ie if string is sanji
, First n will be picked.
EncodedStr will be "n"
, Remaning message is "saji"
, "a" will be picked (In case of even letters, left most middle will be picked)
EncodedStr will be "na", Remaining message is "sji"."j" will be picked.
Encoded Str is "naj"
, Remaining Msg is "si". "s" will be picked. followed by i.Final encoded str will be "najsi"
, and reverse follows for decoding.
Here is my code for encoding and decoding:
char * encode(char *str) {
if (str == NULL)
return NULL;
int len = findLen(str);
int temp = len;
if (len == 1)
return str;
int k = 0;
int mid = 0,i;
int repl_chr = 'A';
char * en = (char*)malloc(sizeof(char)*len);
while (len > 0){
mid = len / 2;
en[k] = str[mid];
k++;
for (i = mid; i < len; i++){
str[i] = str[i + 1];
}
len--;
//memmove(&str[mid], &str[mid + 1], temp - mid);
}
printf("%s",en);
return en;
}
char* decode(char *str) {
if (str == NULL)
return NULL;
int len = findLen(str);
if (len == 1)
return str;
char * en = (char*)malloc(sizeof(char)*len);
return NULL;
}
int findLen(char *str){
int i;
for (i = 0; str[i] != '\0'; i++){}
return i;
}
I have few bugs in my code.Can anyone help me to fix the bugs?