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:
-
The user provides a string s that may contain:
- Alphabets (both uppercase and lowercase)
- Digits (0-9)
- Special characters (which remain unchanged)
-
Apply the following encryption rules:
- Lowercase Characters: Shift by k positions with wrap-around.
- Uppercase Characters: Shift by k positions with wrap-around.
- Digits: Reverse the digit (e.g., 2 becomes 7, 8 becomes 1).
- Special characters remain unchanged.
Encryption Logic:
- Lowercase: s[i] = (s[i] - 'a' + k) % 26 + 'a'
- Uppercase: s[i] = (s[i] - 'A' + k) % 26 + 'A'
- Digit: Reverse by 9 - (s[i] - '0') + '0'
Input Format:
- s: A string that may contain alphabets, digits, and special characters.
- k: An integer representing the shift value.
Output Format:
- Print the encrypted string after applying the shift cipher.
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:
- 1 ≤ length of s ≤ 1000
- 0 ≤ k ≤ 25
Hint:
Follow these steps to solve the problem:
-
Take Input:
- Read the string s that may contain alphabets, digits, and special characters.
- Read an integer k representing the shift value.
-
Iterate Through the String:
- Use a loop to iterate through each character of the string.
- Check the type of character encountered.
-
Check the Character Type:
-
If the character is a lowercase letter (between 'a' and 'z'):
-
If the character is an uppercase letter (between 'A' and 'Z'):
-
If the character is a digit (between '0' and '9'):
-
If the character is a special character (not a letter or digit), leave it unchanged.
-
Print the Result:
- After processing all characters, print the modified string.
Formula Breakdown:
-
To shift lowercase letters:
s[i] = (s[i] - 'a' + k) % 26 + 'a';
-
To shift uppercase letters:
s[i] = (s[i] - 'A' + k) % 26 + 'A';
-
To reverse digits:
s[i] = 9 - (s[i] - '0') + '0';
Task: Implement the above logic and write a C program to encrypt the string.