This program is about decoding secret messages.
the encoded/secret message is written like this (A is replaced with Z), (B with Y), (C with X) and so on.
correct order: 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
secret order: Z, Y, X, W, V, U, T, S, R, Q, P, O, N, M, L, K, J, I, H, G, F, E, D, C, B, A
Examples:
encoded = Svool ---> decoded = Hello
encoded = Sr ---> decoded = HI
encoded = Tllw Wzb ---> decoded = Good Day
encoded = Svb ---> decoded = Hey
encoded = Olevob Nlimrmt ---> decoded = Lovely Morning
what does the following secret message mean?
"ovzimrmt kiltiznnrmt rh nliv vmgvigzrmrmt gszm kozbrmt erwvl tznvh:)"
📢Recommended: Please try to solve it on your own first, before looking at the code down below.
/**
* AUTHOR: Khaled Badran
* DATE: 25/08/2020
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/** the encoded message is written like this (A is replaced with Z), (B with Y), (C with X) and so on,
* i.g encoded = Svool -> decoded = Hello
* so this function replaces the wrong/mysterious order of alphabets with the right one.
*/
char* decode (char* encoded){
int str_length = strlen(encoded);
char* decoded = NULL;
//because of the terminating Null '\0' we will allocate memory for str_length + 1
decoded = calloc(str_length + 1, sizeof(char));
for (int index = 0; index < str_length; index++) {
if (encoded[index] >= 'A' && encoded[index] <= 'Z') { //for upper_case letters.
for(char start = 'A', end = 'Z' ; start <= 'Z' && end >= 'A'; start++, end--)
if (encoded[index] == start) decoded[index] = end;
} else if (encoded[index] >= 'a' && encoded[index] <= 'z') { //for lower_case letters.
for(char start = 'a', end = 'z' ; start <= 'z' && end >= 'a'; start++, end--)
if (encoded[index] == start) decoded[index] = end;
} else //for spaces and any other symbols
decoded[index] = encoded[index];
}
decoded[str_length] = '\0';
return decoded;
}
char* encode(char* normal){
return decode(normal); // decoding a normal message is encoding :) (it is like negative times negative = positive)
}
int main(void){
char* secret_message = "ovzimrmt kiltiznnrmt rh nliv vmgvigzrmrmt gszm kozbrmt erwvl tznvh:)";
char* decoded = decode(secret_message);
printf("The secret message is : %s\n", secret_message);
printf("The decoded message is: %s\n", decoded);
free(decoded); // to free the allocated memory
return 0;
}
___________________________________________________________
output:
The secret message is : ovzimrmt kiltiznnrmt rh nliv vmgvigzrmrmt gszm kozbrmt erwvl tznvh:)
The decoded message is: learning programming is more entertaining than playing video games:)
___________________________________________________________
Comments
Post a Comment