Code:

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

void encryptString(char s[], int k) {
    int n = strlen(s);

    for (int i = 0; i < n; i++) {
        // Check for lowercase letter
        if (s[i] >= 'a' && s[i] <= 'z') {
            s[i] = (s[i] - 'a' + k) % 26 + 'a';
        }
        // Check for uppercase letter
        else if (s[i] >= 'A' && s[i] <= 'Z') {
            s[i] = (s[i] - 'A' + k) % 26 + 'A';
        }
        // Check for digit
        else if (s[i] >= '0' && s[i] <= '9') {
            s[i] = 9 - (s[i] - '0') + '0';
        }
        // Special characters remain unchanged
    }

    // Print the encrypted string
    printf("Encrypted string: %s\n", s);
}

int main() {
    char s[1001];  // String of length up to 1000 + 1 for null terminator
    int k;

    // Get the input string
    printf("Enter the string: ");
    fgets(s, sizeof(s), stdin);

    // Remove trailing newline character added by fgets
    s[strcspn(s, "\n")] = 0;

    // Get the shift value
    printf("Enter the shift value: ");
    scanf("%d", &k);

    // Check constraints for k
    if (k < 0 || k > 25) {
        printf("Invalid shift value! Please enter a value between 0 and 25.\n");
        return 1;
    }

    // Encrypt the string
    encryptString(s, k);

    return 0;
}

Explanation:

Program Overview:

This program encrypts a given string by applying a Caesar cipher to alphabets and reversing digits while keeping special characters unchanged.

Function - encryptString():

The function encryptString() processes the string character by character and applies different transformations based on the type of character.

Character Transformation Logic:

Main Function Workflow:

  1. Input String and Shift Value:
    • The user enters a string and the shift value k for the Caesar cipher.
    • fgets() is used to read the string, and strcspn() removes the trailing newline character.
  2. Validation of Shift Value:
    • A validation check ensures that k is between 0 and 25.
  3. Encryption:
    • The encryptString() function is called to process the input string.
    • Encrypted string is displayed as output.

Edge Cases and Error Handling:

Time Complexity:

Space Complexity: