Search code examples
cfunctionencryptioncaesar-cipher

C - Caesar Cipher [Moving Code to Function]


I am totally new to C programming language, so don't be too critical about my code. I found some tutorials to make Caesar Cipher encryption and decryption program, I just would like to run it from the CLI.

  1. I have code, but it uses global variables and doesn't use functions, i need both of them, i need to somehow move my code to function called "encryption" and another function called "decryption", this all goes to problem number 2

  2. I created encryption code, but i can't make decryption, i understand the main idea, but because i don't have function (Which i need) i can't make the decryption part of the code.

Please help me. Here is my code:

#include<stdio.h>

int main()
{
    char message[100], symbol;
    int i, key;

    printf("Message to encrypt: ");
    gets(message); //fgets is better
    printf("Enter key: ");
    scanf("%d", &key);

    for(i = 0; message[i] != '\0'; ++i){
        symbol = message[i];

        if(symbol >= 'a' && symbol <= 'z'){
            symbol = symbol + key;

            if(symbol > 'z'){
                symbol = symbol - 'z' + 'a' - 1;
            }

            message[i] = symbol;
        }
        else if(symbol >= 'A' && symbol <= 'Z'){
            symbol = symbol + key;

            if(symbol > 'Z'){
                symbol = symbol - 'Z' + 'A' - 1;
            }

            message[i] = symbol;
        }
        else if(symbol >= '0' && symbol <= '9'){
            symbol = symbol + key;

            if(symbol > '9'){
                symbol = symbol - '9' + '1' - 1;    
            }

            message[i] = symbol;
        }
    }

    printf("Encrypted message: %s", message);

    return 0;
}

As i said, i need to make 2 function, one for encryption and another one for decryption, can i get some help ?

Thank you.


Solution

  • The function you need will be something like

    void encryption(char message[100], char encrypted[100], int key)
    

    You can copy paste your code inside that like:

    void encryption(char message[100], char encrypted[100], int key){
       // declare variables here
       for(i = 0; message[i] != '\0'; ++i){
        symbol = message[i];
    
        if(symbol >= 'a' && symbol <= 'z'){
            symbol = symbol + key;
    
            if(symbol > 'z'){
                symbol = symbol - 'z' + 'a' - 1;
            }
    
            encrypted[i] = symbol;
        }
        else if(symbol >= 'A' && symbol <= 'Z'){
            symbol = symbol + key;
    
            if(symbol > 'Z'){
                symbol = symbol - 'Z' + 'A' - 1;
            }
    
            encrypted[i] = symbol;
        }
        else if(symbol >= '0' && symbol <= '9'){
            symbol = symbol + key;
    
            if(symbol > '9'){
                symbol = symbol - '9' + '1' - 1;    
            }
    
            encrypted[i] = symbol;
        }
      }
    }
    

    You don't HAVE to make an array called encrypted[] but since you might need your original message[] later (eg. for checking if your decoding is correct) I would advise against overwriting it.