Task: Vigenère cipher

C: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include <getopt.h>

#define NUMLETTERS 26
#define BUFSIZE 4096

char *get_input(void);

int main(int argc, char *argv[])
{
    char const usage[] = "Usage: vinigere [-d] key";
    char sign = 1; 
    char const plainmsg[] = "Plain text:  ";
    char const cryptmsg[] = "Cipher text: ";
    bool encrypt = true;
    int opt;

    while ((opt = getopt(argc, argv, "d")) != -1) {
        switch (opt) {
        case 'd': 
            sign = -1;
            encrypt = false;
            break;
        default: 
            fprintf(stderr, "Unrecogized command line argument:'-%i'\n", opt);
            fprintf(stderr, "\n%s\n", usage);
            return 1;
        }
    }

    if (argc - optind != 1) {
        fprintf(stderr, "%s requires one argument and one only\n", argv[0]);
        fprintf(stderr, "\n%s\n", usage);
        return 1;
    }


    
    char const *const restrict key = argv[optind];
    size_t const keylen = strlen(key);
    char shifts[keylen];

    char const *restrict plaintext = NULL; 
    for (size_t i = 0; i < keylen; i++) {
        if (!(isalpha(key[i]))) {
            fprintf(stderr, "Invalid key\n");
            return 2;
        }
        char const charcase = (isupper(key[i])) ? 'A' : 'a';
        
        
        shifts[i] = (key[i] - charcase) * sign;
    }

    do {
        fflush(stdout);
        
        
        printf("%s", (encrypt) ? plainmsg : cryptmsg);
        plaintext = get_input();
        if (plaintext == NULL) {
            fprintf(stderr, "Error getting input\n");
            return 4;
        }
    } while (strcmp(plaintext, "") == 0); 

    size_t const plainlen = strlen(plaintext);

    char* const restrict ciphertext = calloc(plainlen + 1, sizeof *ciphertext);
    if (ciphertext == NULL) {
        fprintf(stderr, "Memory error\n");
        return 5;
    }

    for (size_t i = 0, j = 0; i < plainlen; i++) {
        
        if (!(isalpha(plaintext[i]))) {
            ciphertext[i] = plaintext[i];
            continue;
        }
        
        char const charcase = (isupper(plaintext[i])) ? 'A' : 'a';
        
        ciphertext[i] = ((plaintext[i] + shifts[j] - charcase + NUMLETTERS) % NUMLETTERS) + charcase;
        j = (j+1) % keylen;
    }
    ciphertext[plainlen] = '\0';
    printf("%s%s\n", (encrypt) ? cryptmsg : plainmsg, ciphertext);

    free(ciphertext);
    
    free((char*) plaintext);
    return 0;
}
char *get_input(void) {

    char *const restrict buf = malloc(BUFSIZE * sizeof (char));
    if (buf == NULL) {
        return NULL;
    }

    fgets(buf, BUFSIZE, stdin);

    
    size_t const len = strlen(buf);
    if (buf[len - 1] == '\n') buf[len - 1] = '\0';

    return buf;
}











Ada: WITH Ada.Text_IO, Ada.Characters.Handling;
USE Ada.Text_IO, Ada.Characters.Handling;

PROCEDURE Main IS
   SUBTYPE Alpha IS Character RANGE 'A' .. 'Z';
   TYPE Ring IS MOD (Alpha'Range_length);
   TYPE Seq IS ARRAY (Integer RANGE <>) OF Ring;
   
   FUNCTION "+" (S, Key : Seq) RETURN Seq IS
      R : Seq (S'Range);
   BEGIN
      FOR I IN R'Range LOOP
         R (I) := S (I) + Key (Key'First + (I - R'First) MOD Key'Length);
      END LOOP;
      RETURN R;
   END "+";
   
   FUNCTION "-" (S : Seq) RETURN Seq IS
      R : Seq (S'Range);
   BEGIN
      FOR I IN R'Range LOOP
         R (I) := - S (I);
      END LOOP; 
      RETURN R;
   END "-";
   
   FUNCTION To_Seq (S : String) RETURN Seq IS
      R  : Seq (S'Range);
      I  : Integer := R'First;
   BEGIN
      FOR C OF To_Upper (S) LOOP
         IF C IN Alpha THEN
            R (I) := Ring'Mod (Alpha'Pos (C) - Alpha'Pos (Alpha'First));
            I := I + 1;
         END IF;
      END LOOP;
      RETURN R (R'First .. I - 1);
   END To_Seq;
   
   FUNCTION To_String (S : Seq ) RETURN String IS
      R : String (S'Range);
   BEGIN
      FOR I IN R'Range LOOP
         R (I) := Alpha'Val ( Integer (S (I)) + Alpha'Pos (Alpha'First));
      END LOOP;
      RETURN R;
   END To_String;
   
   Input : Seq := To_Seq (Get_Line);
   Key : Seq := To_Seq (Get_Line);
   Crypt : Seq := Input + Key;
BEGIN
   Put_Line ("Encrypted: " & To_String (Crypt));
   Put_Line ("Decrypted: " & To_String (Crypt + (-Key)));
END Main;