RE: Encoder/Decoder Challenge/Source 09-19-2012, 01:32 AM
#3
How is it a timestamp when it's an user entered number? Timestamp is a number that determines some time and date, but the way you attempt to use it in this code makes it a password/key, it's not a timestamp. When you use it to initialize the random number generator, it's called a seed.
I see a bunch of weird things in this code. Like... why do you declare the function encode() as returning char, when you just return 0 (null character) in the end? What's the purpose of that? Why does it always return the same value that's basically useless? If you don't need to return any value from a function, just declare it as a void.
However... this function should return something and that something should be the the encoded/decoded (although better term would be encrypted/decrypted) string. Your function prints it instead, which is a very bad thing to do. It says "encode" so it should do that, nothing else.
A rule of thumb for writing a good code is - functions should never do anything in addition to what they're supposed to do, ideally they should have no side effects.
If the function is supposed to encode some string, it should do that and return the encoded string, nothing else. What if you wanted to store it somewhere instead? Using it internally? You can't, because it doesn't return it to you, it just prints in on the screen...
Instead, you should encode it and return it and then print the encoded string if you want. There are multiple ways to do that, either dynamically allocate memory in the function, write the encoded string into it and return a pointer to it, however then you have to remember to free this memory outside of the function.
The other, more traditional way of doing this is by passing a pointer to a buffer where to store the encoded/decoded string and optionally also providing maximum length for safety, like this:
And write the encoded string to the buffer inside of a function.
Also you can simplify the
by writing
Basically any value that's non-zero is considered true and '\0' is equal the number zero, so when the character at index i is zero, the loop will stop.
Anyway, I didn't look at your code much in detail, these are only things that I've noticed at first glance, hope this helps you to improve and others too.
I see a bunch of weird things in this code. Like... why do you declare the function encode() as returning char, when you just return 0 (null character) in the end? What's the purpose of that? Why does it always return the same value that's basically useless? If you don't need to return any value from a function, just declare it as a void.
However... this function should return something and that something should be the the encoded/decoded (although better term would be encrypted/decrypted) string. Your function prints it instead, which is a very bad thing to do. It says "encode" so it should do that, nothing else.
A rule of thumb for writing a good code is - functions should never do anything in addition to what they're supposed to do, ideally they should have no side effects.
If the function is supposed to encode some string, it should do that and return the encoded string, nothing else. What if you wanted to store it somewhere instead? Using it internally? You can't, because it doesn't return it to you, it just prints in on the screen...
Instead, you should encode it and return it and then print the encoded string if you want. There are multiple ways to do that, either dynamically allocate memory in the function, write the encoded string into it and return a pointer to it, however then you have to remember to free this memory outside of the function.
The other, more traditional way of doing this is by passing a pointer to a buffer where to store the encoded/decoded string and optionally also providing maximum length for safety, like this:
Code:
void Encode(char *toEncode, char *buffer, int maxsize);And write the encoded string to the buffer inside of a function.
Also you can simplify the
Code:
while(toEncode[i] != '\0')Code:
while(toEncode[i])Basically any value that's non-zero is considered true and '\0' is equal the number zero, so when the character at index i is zero, the loop will stop.
Anyway, I didn't look at your code much in detail, these are only things that I've noticed at first glance, hope this helps you to improve and others too.
I love creativity and creating, I love science and rational thought, I am an open atheist and avid self-learner.




![[+]](https://sinister.li/images/modern/collapse_collapsed.png)