Assignment #3

Due date: 10AM one week after the assignment was handed out

Required Reading: Chapters 2,3,4,5 [IACU]


Purpose

The main purposes for this assignment are to introduce you to:
  1. more flow control Dynamic memory allocation
  2. string manipulation
  3. pointers manipulation
  4. comments !!!
  5. README file !!!
Try to keep your files organized. For example create a directory for this class , create a sub directory for assignment 3.

Write the first program in a file called "p1.c", This way your files and executables will be organized. Do the same with p2.


Programming (100 points)

  1. Exercise (50 points)
  2. You are a programmer that works for the intelligence unit in the country Citomania. Your supervisor told you that you have to build an encoder for all outgoing documents, since the relations with the country Bitomania are not as friendly as they used to be, and the king of Citomania is worried. You need to build a robust encoder, with encoding functions that you'll be able to replace periodically. Here are the instructions your supervisor gave you: You need to implement five different encoding functions, all have the same prototype: char * encode_func(char *s), they all operate on a string s and return the new encoded string.
    Pay attention: these functions might output a bigger string. In these cases, you will need to allocate more space for the string s (you should use the function realloc).
    You can assume that all strings contain only letters and digits.

    0. Re-write the string so that for every letter you write the following letter in the alphabet (where A becomes B and z becomes a), for every digit write the following digit, (0 becomes 1 and 9 becomes 0). Then append at the end of the string a ":" character and the numerical value which is the sum of the ASCII values of the original string characters, modulo 7.
    Example: "Af2t9" becomes "Bg3u0:5" since the ASCII sum of the characters is 390.

    1. Same as function 0 but write the previous letter/number instead of the following one. Then append at the end of the string a ":" character and the numerical value which is the sum of the ASCII values of the original string characters, modulo 3.
    Example: "Af2t9" becomes "Ze1s8:0"since the ASCII sum of the characters is 390.

    2. Create a palindrome out of the input string. Example: "AbX" becomes "AbXXbA".
     

    3. Rewrite the string so that there is a substring character "22" in-between every two characters.
    Example: "ABC" becomes "A22B22C". Don't insert this substring at the beginning or at the end of the string
    (so the encodings "22A22B221C" or "A22B221C22" are wrong!).

    4. Reverse the string s and insert the character '1' at the beginning and at the end of the string. Example: "abcde" becomes "1edcba1".

    Now that you're done with writing the encoding functions, here's how/when to use them:
    We will use a method similar to hashing (if you study database systems, you will study about hash tables).
    There is a special "string code" that decides which encoding function to use on each string: For every string s, sum the ASCII values of its characters (not including the '\0'), and then compute its value modulo five. This value is the decision number for the encryption function and has the values 0..4. Example: given the string "AB" the sum of ASCII values is 65+66=131. Now, 131 % 5 == 1, so you should use the second encryption function, and the output of your program should be "ZA".

    Now some instructions from your instructor:
    Define the string s as a pointer to a character: char *s. You first ask the user to enter the number of characters the string has, and you use this number to dynamically allocate enough memory to the string s, using the dynamic allocation functions we discussed in class. Your program then reads a string from the user into s, applies the right encoding function on s, and prints the encoded string to the screen.
    Create an array of pointers to functions, where the function prototype is: char * encode_func(char *).
    You first define an array of five pointers to pointers to functions:
    char *(*FUNC[5])(char *);
    Then initialize the array with the five decoding functions above (after implementing them...).
    Your program should read a string from the user and compute its "string code" (a value between 0..4). Then it should use this code as an index to the array of functions, and apply the right function on the input string. You should then print the output string to the screen.
     

  3. Exercise (50 points)
  4. You are a programmer in the intelligence unit in Bitomania. It has been known for quite a while in your unit about the upcoming war and the plans to concur Citomania. Your commander just told you that the Citomania are really dumb and that they tried to build an encoder so that the Bitomania couldn't read their important top secret papers. He told you that he has the list of the current 5 encoding functions that are being used in Citomania, and that you should build the Bitomania decoder. You're afraid that if you don't succeed your commander will call you a "dumb Citomania" which is, in your mind, the most terrible thing that could happen to you. So you go on and write decoding functions to be applied on a string, they have the same prototype as the Citomania decoding functions, and you even build an array of five pointers to functions. All you can do now is given a string, figure which encoding function was applied to it, and then apply the matching decoding function on it and print it to the screen. If no encoding was detected, a notification that Citomania has changed its coding system should be printed.
    Follow the instructions for exercise #1, in fact, you can reuse most of the code.
    Example: given a string "abc:0", there are only two immediate options for the encoding function used, depending on the sum of ASCII values of the letters a,b,c. Here the sum is 294 which gives 0 both for modulo 7 and modulo 3. In this case apply BOTH decoding functions and print TWO strings. (I never said the encoding was perfect...) Remember: once you use your decoding function on a string that string is changed, you cannot re-use it (so create a copy for this case).

    Extra points: is there a way to know which decoding function to use in the case of "acb:0"?

    Don't worry about realloc here!


    Arranging your file:

    Write all five encoding functions in a file encode.c, Write a file encode.h with prototypes of these functions. Write all five decoding functions in a file decode.c, Write a file decode.h with prototypes of these functions. Write the first main function that uses the encoding functions in p1.c, and the second main function that uses the decoding functions in p2.c.