Thread: split into two c files and one header file.

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    4

    split into two c files and one header file.

    For this part, I want you to revisit your Caesar encryption code from assignment 3 and modify it
    so it contains at least two .c files: a driver containing the main function and a support file
    containing a function that runs your Caesar encryption code. These files will be linked by a
    header file (.h file).
    The main function shall prompt the user to enter a string to encrypt and an encryption key to
    offset the string values. These values are then passed the caesarEncrypt function, which encrypts
    the string and prints out the value, and returns the number of characters in the string as check for
    the user. A suggested caesarEncrypt prototype is int caesarEncrypt(const char inputString[], int
    key);
    The file caesar.h contains the function prototype for your Caesar encryption function and any
    other functions located in caesar.c. In caesar.c and main.c, link the file together with common
    header #include “caesar.h”
    This header file with give main.c the definitions of the prototypes in declared in caesar.c.
    Your header will use macros to ensure that files are only included once.

    Code:
    #include <stdio.h>
    
    
    #define MAX 80
    #define UPPER_ENCRYPT ((ch - 'A') + offset) % 26 + 'A'
    #define LOWER_ENCRYPT ((ch - 'a') + offset) % 26 + 'a'
    int len, offset;
    char message[MAX], c;
    
    
    int main(void)
    {
    
    
            printf("Enter message to be encrypted: ");
            for (len = 0; (c = getchar()) != '\n'; len++) {
                    message[len] = c;
            }
    
    
            printf("Enter shift amount (1-25): ");
            scanf("%d", &offset);
    
    
            printf("Encrypted message: ");
            for (int i = 0; i < len; i++) {
                    char ch = message[i];
    
    
                    if (message[i] >= 'a' && message[i] <= 'z') {
                            ch = LOWER_ENCRYPT;
                    } else if (message[i] >= 'A' && message[i] <= 'Z') {
                            ch = UPPER_ENCRYPT;
                    }
                    printf("%c", ch);
            }
            printf("\n");
    
    
            return 0;
    }
    
    


    Help me please.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Step 1: Create the function caesarEncrypt in the same file as the main function.
    But, create it after the main function.

    Step 2: Split into three files.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jan 2018
    Posts
    4
    Well I have no idea how to handle this. Can you do more specific?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I find these encryption assignments fascinating. They're very common and nearly invariably the students implementing them do stuff like
    Code:
    if (message[i] >= 'a' && message[i] <= 'z') {
    

    which is implementation dependent. I guess it's entrenched now

  6. #6
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    Quote Originally Posted by Hodor View Post
    I find these encryption assignments fascinating. They're very common and nearly invariably the students implementing them do stuff like
    Code:
    if (message[i] >= 'a' && message[i] <= 'z') {
    

    which is implementation dependent. I guess it's entrenched now
    An interesting standard header ctype.h
    For instance:

    Code:
    if( isalpha(ch) && islower(ch) )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File handling and header files?
    By linkstatic in forum C++ Programming
    Replies: 3
    Last Post: 12-20-2011, 03:18 PM
  2. Replies: 6
    Last Post: 10-04-2011, 10:17 PM
  3. compiling source files and header file at once?
    By 1jackjack in forum C Programming
    Replies: 10
    Last Post: 05-04-2007, 11:06 AM
  4. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  5. #define Header files. End Of File Errors.
    By bartybasher in forum C Programming
    Replies: 8
    Last Post: 07-28-2003, 03:03 PM

Tags for this Thread