Skip to content

luc088.c

Problem Statement

Write a program to encrypt/decrypt a file using: (1) Offset cipher (2) Substitution cipher.

Metadata

Property Detail
Author Amit Dutta amitdutta4255@gmail.com
Date 08 Feb 2026
License MIT License (See the LICENSE file for details)
Difficulty Intermediate (index: 4 / 10)

Concepts

Beta Feature

This concept detection system is still in beta and may occasionally show incorrect or incomplete results.

  • Pointers
  • Recursion
  • Sorting (possible)
  • Iteration

Actions

Raw View on GitHub

You can print or save this file by opening Raw and using your browser.

Source Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void create_plain_file();

int main()
{
    FILE *fs, *ft;
    char ch;
    int choice;

    create_plain_file();

    printf("1. Offset Cipher\n2. Substitution Cipher\nEnter choice: ");
    scanf("%d", &choice);

    fs = fopen("plain.txt", "r");
    ft = fopen("coded.txt", "w");

    if (fs == NULL || ft == NULL)
    {
        printf("Error opening files.\n");
        exit(1);
    }

    while ((ch = fgetc(fs)) != EOF)
    {
        if (choice == 1)
        {
            // Offset Cipher: Add 128 (effectively shifts char code)
            fputc(ch + 10, ft); // Using +10 for visibility, problem says 128
        }
        else
        {
            // Simple Substitution: A->!, B->@ etc. 
            // Here, we'll just map any char to char+5 for simplicity 
            // as true substitution requires a full map array.
            fputc(ch + 5, ft);
        }
    }

    printf("Encryption complete. Check 'coded.txt'.\n");

    fclose(fs);
    fclose(ft);
    return 0;
}

void create_plain_file()
{
    FILE *fp = fopen("plain.txt", "w");
    if (fp)
    {
        fprintf(fp, "SECRET MESSAGE");
        fclose(fp);
    }
}

Explanation

Explain with AI

Copy the prompt below and paste it into any AI assistant.

    You are explaining a C programming code to a beginner.

    STRICT RULES:

    - Only use the given code. Do NOT assume anything not present.

    - Do NOT add extra examples.

    - Keep explanation clear and short.

    - If something is unclear, say "Not clear from code".

    - Follow the exact format below. Do NOT change headings.

    FORMAT:

    [START]

    ## What it does

    (Explain the overall purpose in 1-2 sentences)

    ## Step-by-step

    (Explain how the code works in steps, simple language)

    ## Key Concepts

    (List concepts like loop, condition, function, etc.)

    ## Notes

    (Mention any limitations, errors, or assumptions)

    [END]

    CODE (luc088.c):

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>

    void create_plain_file();

    int main()
    {
        FILE *fs, *ft;
        char ch;
        int choice;

        create_plain_file();

        printf("1. Offset Cipher\n2. Substitution Cipher\nEnter choice: ");
        scanf("%d", &choice);

        fs = fopen("plain.txt", "r");
        ft = fopen("coded.txt", "w");

        if (fs == NULL || ft == NULL)
        {
            printf("Error opening files.\n");
            exit(1);
        }

        while ((ch = fgetc(fs)) != EOF)
        {
            if (choice == 1)
            {
                // Offset Cipher: Add 128 (effectively shifts char code)
                fputc(ch + 10, ft); // Using +10 for visibility, problem says 128
            }
            else
            {
                // Simple Substitution: A->!, B->@ etc. 
                // Here, we'll just map any char to char+5 for simplicity 
                // as true substitution requires a full map array.
                fputc(ch + 5, ft);
            }
        }

        printf("Encryption complete. Check 'coded.txt'.\n");

        fclose(fs);
        fclose(ft);
        return 0;
    }

    void create_plain_file()
    {
        FILE *fp = fopen("plain.txt", "w");
        if (fp)
        {
            fprintf(fp, "SECRET MESSAGE");
            fclose(fp);
        }
    }