Encryption and Decryption

This is a discussion on Encryption and Decryption within the C Programming forums, part of the General Programming Boards category; I have a school assignment in which asks me to create a program that does the following on encryption and ...

Encryption and Decryption

I have a school assignment in which asks me to create a program that does the following on encryption and decryption:

Write a program that encrypts and decrypts a text (string). The program reads a text into a character pointer: char * originaltext. Write a function that encrypts originaltext and store the encrypted text into: char * ciphertext. The encryption algorithm is replacing each character by a character having 10 added to its ASCII code. Write another function that decrypts ciphertext and store the result into: char * deciphertext. The decryption algorithm is replacing each character by a character having 10 subtracted from its ASCII code. Display the three strings originaltext, ciphertext,and deciphertext.

It then gives the following hints to start the program:

Use malloc or calloc functions to allocate dynamically a memory space for strings.

Assume that the text has a maximum of 100 characters.

Donít forget to deallocate the memory using free function.

Could somebody help me to write the program, I'm really stuck and don't know how to start.

Could somebody help me to write the program, I'm really stuck and don't know how to start.

We don't do people's homework for them. You must at least know how to create a main() function. And I doubt the instructor would have you using dynamic memory allocation without going over it first. Come on, at least try.

1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

1) originalString is a pointer pointing to neverland. It is never initialized to point to a memory location that has enough space to hold 100 characters. Therefore you can't read in it. Also, don't use scanf to read strings. Use fgets().

2) don't cast malloc

3) other functions don't belong inside the main function, they belong above.

4) *ciphertext = *originaltext + 10;

This is wrong, I'll let you figure out what *ciphertext and *originaltext really are. (Hint: What is their type?)

"It's most likely wrong since it gives me a blank window screen"

This is not a trial and error exercise. You have to tell us what is wrong by COMPILING THE CODE. If you were a bridge engineer you wouldn't go: try to drive over, although this is most likely wrong and you will die trying.

1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

I'm using *ciphertext = *originaltext + 10; for encrypt because it's supposed to lead to each character of a string replaced by a character having 10 added, and I'm using *deciphertext = *ciphertext - 10: for decrypt as it's supposed to be the previous *ciphertext subtracted by 10 (I would think that this means *deciphertext points the same as *originaltext)