Question
Kattis - DRM Messages - C Language I am having problems with the 'Rotate' part, where each character in each of the 2 halves are
Kattis - DRM Messages - C Language
I am having problems with the 'Rotate' part, where each character in each of the 2 halves are rotated n positions forward. My code works for some alphabets but not for others.
#include
#include
void rotate(int, int, char []);
int main(void) {
int i, len, j = 0;
char str[15001], back[7501];
scanf("%s", str);
len = strlen(str);
/*for(i = len / 2; i
back[j++] = str[i];
back[j] = '\0';*/
rotate(0, len / 2, str);
rotate(len / 2, len, str);
printf("str is %s ", str);
return 0;
}
void rotate(int start, int limit, char str[]) {
int i, value = 0;
for(i = start; i
value += (str[i] - 'A');
printf("value %d ", value);
for(i = start; i
printf("i %d ", i);
printf("str[i] is %c, before adding rot %d ", str[i], str[i]);
str[i] += value;
printf("after adding rot %d ", str[i]);
while(str[i] > 'Z')
str[i] -= 26;
}
}
DRM Messages DRM Encryption is a new kind of encryption. Given an encrypted string (which we'll call a DRM message), the decryption process involves three steps: Divide, Rotate and Merge. This process is described in the following example with the DRM message "EWPGAJRB" Divide - First, divide the message in half to "EWPG" and "AJRB". Rotate For each half, calculate its rotation value by summing up the values of each character ( A = 0, B 1, . . . , z 25). The rotation value of "EWPG" is 4 + 22 + 15 + 6-47. Rotate each character in "EWPG" 47 positions forward (wrapping from Z to A when necessary) to obtain the new string "ZRKB" Following the same process on "AJRB" results in "BKSC" Merge The last step is to combine these new strings ("ZRKB" and "BKSC") by rotating each character in the first string by the value of the corresponding character in the second string. For the first position, rotating 'Z' by 'B' means moving it forward 1 character, which wraps it around to 'A'. Continuing this process for every character results in the final decrypted message, "ABCD". Input The input contains a single DRM message to be decrypted. All characters in the string are uppercase letters and the string's length is even and 15 000. Output Display the decrypted DRM message. Sample Input 1 Sample Output 1 EWPGAJRB ABCD Sample Input 2 Sample Output 2 UEQBJPJCBUDGBNKCAHXCVERXUCVK ACMECNACONTESTStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started