// Arup Guha
// 8/18/2025
// Flip "cipher" implementation example

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char* encrypt(char* plain);

int main() {

    // Get user's message.
    char msg[100];
    printf("enter message\n");
    scanf("%s", msg);

    // Encrypt the input, must be all lowercase letters.
    char* res = encrypt(msg);
    printf("%s\n", res);

    // Call encrypt again - what should we get? Why?
    char* res2 = encrypt(res);
    printf("%s\n", res2);

    return 0;
}

// Assumes plain is lowercase letters only.
char* encrypt(char* plain) {

    int n = strlen(plain);
    char* res = malloc(sizeof(char)*(n+1));

    // Go through the string.
    for (int i=0; i<n; i++) {

        // This is the numeric value of the number to encrypt.
        int plainNum = plain[i] - 'a';

        // Mathematically, this is what we're doing to encrypt.
        int cipherNum = 25 - plainNum;

        // Now store this back as a letter.
        res[i] = (char)('a'+cipherNum);
    }

    // Null terminate.
    res[n] = '\0';

    // Ta da!
    return res;
}
