Cranes at VIIT Q2

String Encryption with Shift Cipher

Write a C program that encrypts a given string using a modified shift cipher with specified conditions.

Problem Statement:

Encryption Logic:

Input Format:

Output Format:

Example Inputs & Outputs:

Enter the string: Hello123
Enter the shift value: 3
Encrypted string: Khoor876
            
Enter the string: Cipher99!
Enter the shift value: 5
Encrypted string: Hnumjw00!
            

Constraints:

Hint:

Follow these steps to solve the problem:

  1. Take Input:
    • Read the string s that may contain alphabets, digits, and special characters.
    • Read an integer k representing the shift value.
  2. Iterate Through the String:
    • Use a loop to iterate through each character of the string.
    • Check the type of character encountered.
  3. Check the Character Type:
    • If the character is a lowercase letter (between 'a' and 'z'):
      • Shift it by k positions with wrap-around.
      • Use the formula:
        s[i] = (s[i] - 'a' + k) % 26 + 'a';
    • If the character is an uppercase letter (between 'A' and 'Z'):
      • Shift it by k positions with wrap-around.
      • Use the formula:
        s[i] = (s[i] - 'A' + k) % 26 + 'A';
    • If the character is a digit (between '0' and '9'):
      • Reverse the digit using:
        s[i] = 9 - (s[i] - '0') + '0';
    • If the character is a special character (not a letter or digit), leave it unchanged.
  4. Print the Result:
    • After processing all characters, print the modified string.

Formula Breakdown:

Task: Implement the above logic and write a C program to encrypt the string.