-2

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.

shanji97
  • 71
  • 1
  • 7
  • 19

2 Answers2

2

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.

phan801
  • 227
  • 1
  • 9
1

Encryption function

char * encryption(char * message,  int key)
{
  char * encrypted_message= (char *)malloc( (strlen(message)+1)*sizeof(char)); //create a new message that has the length of the original message + 1 space for the null character ('\0')

 //do the encryption

 return encrypted_message;
  //or make it void and print it the enc. msg. or both
}

You can also include string.h lib and use the strlen() function to get the length of the string.

shanji97
  • 71
  • 1
  • 7
  • 19
  • `strlen` would just add another iteration over the string, so iterating until finding the null character is preferred imho. – Nicolas Dusart Dec 12 '17 at 00:17
  • I agree, but in my opinion I'd rather waste cpu time than memory. Are there any guidelines when to use memory or CPU? – shanji97 Dec 12 '17 at 10:25