Assignment VII



  1. In the rail fence cipher, the plaintext is written downwards and diagonally on successive "rails" of an imaginary fence, then moving up when we reach the bottom rail. When we reach the top rail, the message is written downwards again until the whole plaintext is written out. The message is then read off in rows. For example, if we have 3 "rails" and a message of 'this is a test', the cipherer writes out:
    t       i       e
      h   s   s   t   s
        i       a       t
    
    The encrypted text then reads as:
    tie hssts iat
    
    Write a C program to do the following:
    1. Read the number of rails (int no_of_rails) and the message stream in a character array (char sm[]). The characters should be read one by one and filled into the array until EOF. Assume that the maximum number of characters that can be read is 100.
    2. Display the rail fence using the function void display(char sm[], int no_of_rails).
    3. Write a function void encrypt(char sm[], char c[], int no_of_rails) which encrypts the meassage in sm[] using rail fence cipher and stores the result in c[]. The cipher text then should be printed from the main function.
    4. Write a function void decrypt(char c[], char dm[], int no_of_rails) that will decrypt (i.e., given the cipher c[], shall obtain the original message back) and store the original message in dm[] (ignoring the position of the white spaces this time). The decrypted result should be printed from the main function.
    Your program should ask the user whether he/she wants to (0) Display (1) Encrypt (2) Decrypt or (3 and above) exit from the program.
    Example execution:
    Enter number of rails: 3
    Enter the plaintext:> 
    this is a test
    Enter your choice: 0
    
    t       i       e
      h   s   s   t   s
        i       a       t
    
    Enter your choice: 1
    Cipher text: tie hssts iat
    Enter your choice: 2
    Decrypted text: thisisatest
    Enter your choice: 3
    
  2. Write a C program to implement a function that finds out the longest word in a text stream (terminated by EOF) and returns the length of the string. Both the longest word and the length should be printed from the main function. If there are two or more words that have the same length and qualify to be the longest, print the first one.
    Sample execution:
    Enter the text stream:>
    Indian Institute of Technology, Kharagpur
    The longest word is: Technology,
    The length of the longest word is: 11
    
    BONUS QUESTIONS (not to be evaluated as a component of this assignment)

  3. Write a function int LowerToUpper(char *source, char *convert) and int UpperToLower(char *source, char *convert) that converts a given stream of characters (terminated by EOF) from lower case to upper case and vice versa (do not use any standard inbuilt String Functions) and print the converted characters and the number of characters converted from the main function. The program should provide the user with the options (0) LowerToUpper (1) UpperToLower (3 and above) exit.
  4. Write a recursive function int charSum(char *s) that returns the number of vowel characters present in the string *s. Print the sum from the main function.