In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of April 7th
(this exercise will still be available for submission after that deadline, but without couting towards your grade)
[to understand the context of this problem, you should read the class #05 exercise sheet]
In this problem you should submit a file containing the function as described (without any main function). Inside the function do not print anything that was not asked!
Jaime Lannister, the famous knight of the Kingsguard, needs to send a secret message to his army stationed at the battlefront. To ensure that the message remains confidential if intercepted, Jaime decides to use the Caesar cipher, a simple encryption technique. In a Caesar cipher, each letter in the plaintext is shifted a fixed number k of places down the alphabet.
Imagine for instance that k=10. Then the following letter substitions would occur (with each letter becoming the letter 10 positions to the left in the alphabet, wrapping around):
old letter: abcdefghijklmnopqrstuvwxyz new letter: klmnopqrstuvwxyzabcdefghij
For example, with this k, "attack" would become "kddkmu".
Write a function void caesar_cipher(char str[], int k) that, given a string str and an integer k, changes the string str so that all its (lowercase) letters are shifted to the right by k positions, as previously explained. Non-alphabetic characters (e.g., digits, punctuation, spaces) should remain unchanged.
The following limits are guaranteed in all the test cases that will be given to your program:
1 ≤ |str| < 100 | Length of the string | |
1 ≤ k < 26 | Number of positions to shift |
You can also be assured that all letters in the string are in lowercase.
You should submit a .c
file containing the requested function, without any main function and without printing anything. You can however create additional methods, if you need them.
Mooshak will use the following code to link to your function, read the inputs and call your method, printing its result.
#include <stdio.h> #define MAX_SIZE 100 void caesar_cipher(char [], int); int main(void) { // Read amount of cases int n; scanf("%d", &n); // Read n cases and for each call the caesar_cipher function char str[MAX_SIZE]; int k; for (int i=0; i<n; i++) { scanf("%s %d", str, &k); printf("caesar_cipher(\"%s\", %d) -> ", str, k); caesar_cipher(str, k); // call function printf("\"%s\"\n", str); // show resulting string } return 0; }
Example Input | Example Output |
3 attack_the_starks 10 winteriscoming 23 tfkqbofpzljfkd 3 |
caesar_cipher("attack_the_starks", 10) -> "kddkmu_dro_cdkbuc" caesar_cipher("winteriscoming", 23) -> "tfkqbofpzljfkd" caesar_cipher("tfkqbofpzljfkd", 3) -> "winteriscoming" |