Encrypt/Decrypt Text (Caesar Cipher) in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>

int main() {
    char c; int shift = 3;
    FILE *f = fopen("text.txt", "r");
    while ((c = fgetc(f)) != EOF) {
        if (c >= 'a' && c <= 'z') c = 'a' + (c - 'a' + shift) % 26;
        else if (c >= 'A' && c <= 'Z') c = 'A' + (c - 'A' + shift) % 26;
        putchar(c);
    }
    fclose(f);
}

C Output

Input:  
Hello World 

Output:  
Khoor Zruog


C++ Program

#include <iostream>
using namespace std;

int main() {
    string s = "Hello World"; int k = 3;
    for (char &c : s) {
        if (isalpha(c)) {
            char base = isupper(c) ? 'A' : 'a';
            c = base + (c - base + k) % 26;
        }
    }
    cout << s;
}

C++ Output

Input:  
Hello World 

Output:  
Khoor Zruog


JAVA Program

public class Main {
    public static void main(String[] args) {
        String s = "Hello World"; int k = 3;
        StringBuilder r = new StringBuilder();
        for (char c : s.toCharArray()) {
            if (Character.isLetter(c)) {
                char b = Character.isUpperCase(c) ? 'A' : 'a';
                r.append((char)(b + (c - b + k) % 26));
            } else r.append(c);
        }
        System.out.println(r);
    }
}

JAVA Output

Input:  
Hello World 

Output:  
Khoor Zruog


Python Program

s = "Hello World"; k = 3
print(''.join(chr((ord(c)-65+k)%26+65) if c.isupper()
      else chr((ord(c)-97+k)%26+97) if c.islower() else c for c in s))

Python Output

Input:  
Hello World 

Output:  
Khoor Zruog


In-Depth Explanation
Example
If you begin with the word "Hello" and use a Caesar Cipher with a shift of 3, every letter is shifted three places ahead in the alphabet.
So:

H → K
e → h

l → o

l → o

o → r

Therefore, "Hello" gets transformed to "Khoor".

Real-Life Analogy
Suppose you had a secret wheel with all the letters on the outer rim equating to a letter moved on the inner rim. You just turn the wheel 3 turns to the right, and voila. All messages are jumbled. The method was adopted by Julius Caesar to dispatch confidential military orders—thus the name.

This is precisely how simple substitution ciphers operate, and Caesar Cipher is the most primitive. In contemporary times, although antiquated, it is the foundation upon which more complex encryption algorithms are developed.

Why It Matters
Encryption and decryption are critical in cybersecurity. Although the Caesar Cipher is primitive and insecure by today's standards, it introduces fundamental programming ideas such as:

Character encoding and manipulation

ASCII values and modular arithmetic

String parsing

Logic design for symmetric transformations

It also shows how a transformation (encryption) can be reversed (decryption) by reversing the operation. If shift +3 encrypts, shift -3 decrypts.

Learning Insights
With this small and effective implementation, new programmers learn:

How to handle characters as numbers

How alphabets wrap around using % 26 logic

How to maintain case sensitivity (upper/lower case)

How to ignore non-letter characters (such as space, punctuation)

The reversible nature of deterministic transformations

In C and C++, we call putchar, whereas in Java and Python we construct new strings. These are tiny differences that reflect language-specific I/O and character handling.

Interview and Real-World Application
Caesar Cipher is usually a standard starting interview problem. Interviewers can ask:

"Can you encrypt this string with Caesar Cipher?"

"How do you reverse it?"

"How do you apply it to half of a file?"

It's also applied in programming competitions, cyber security training, and education tools to introduce encryption fundamentals.

Although Caesar Cipher is not practiced in contemporary cryptography, its value rotation logic and application of mod math appear in actual encryption algorithms such as ROT13, AES key-scheduling, JWT token creation, and password hashing algorithms—demonstrating it as a great conceptual entry point.

Encrypting and decrypting text with Caesar Cipher is an introductory exercise that acquaints newcomers with the realm of cryptography, ASCII manipulation, and logic programming. This is a fundamental concept that is helpful to study so that one can better comprehend how real-world encryption operates and is commonly applied in newbie interviews, cybersecurity contests, and course studies. The above programs provide the most straightforward and simplest means of carrying out Caesar encryption in C, C++, Java, and Python and is, therefore, best suited for students and novice developers to learn practical coding skills using an actual-world scenario of encryption.