1.
You are tasted with rotate _ image
#include <stdio.h>
// Function to rotate the image anticlockwise
void rotate_image(int size, int img[size][size]) {
for (int layer = 0; layer < size / 2; ++layer) {
int first = layer;
int last = size - 1 - layer;
for (int i = first; i < last; ++i) {
int offset = i - first;
// Save top
int top = img[first][i];
// Left to top
img[first][i] = img[i][last];
// Bottom to left
img[i][last] = img[last][last - offset];
// Right to bottom
img[last][last - offset] = img[last - offset][first];
// Top to right
img[last - offset][first] = top;
}
}
}
int main() {
int size;
scanf("%d", &size);
int img[size][size];
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
scanf("%d", &img[i][j]);
}
}
rotate_image(size, img);
// Print the rotated image without spaces after the last element
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
printf("%d", img[i][j]);
if (j < size - 1) {
printf(" ");
}
}
printf("\n");
}
return 0;
}
2.Task: Decipher Encrypted Text write decipher Function that takes the encrypted
strings as input and return the original, encrypted string.
Objective: Write a function decipher to determine the original string fro8m a given
ciphertext and a known word using a Caesar cipher decryption method
Answer:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_LEN 1000
// Helper function to decrypt a character with a given shift
char decrypt_char(char c, int shift) {
if (isalpha(c)) {
char base = islower(c) ? 'a' : 'A';
return (c - base - shift + 26) % 26 + base;
}
return c;
}
// Function to decrypt the entire string with a given shift
void decrypt_string(char *ciphertext, char *decipheredString, int shift) {
int len = strlen(ciphertext);
for (int i = 0; i < len; ++i) {
decipheredString[i] = decrypt_char(ciphertext[i], shift);
}
decipheredString[len] = '\0';
}
char *decipher(char ciphertext[], char knownWord[]) {
static char decipheredString[MAX_LEN];
int cipher_len = strlen(ciphertext);
int known_len = strlen(knownWord);
for (int shift = 0; shift < 26; ++shift) {
decrypt_string(ciphertext, decipheredString, shift);
if (strstr(decipheredString, knownWord) != NULL) {
return decipheredString;
}
}
return "Invalid";
}
int main() {
char ciphertext[MAX_LEN];
char knownWord[MAX_LEN];
// Input the ciphertext
scanf("%[^\n]%*c", ciphertext);
// Input the knownWord
scanf("%s", knownWord);
// Call the decipher function
char *result = decipher(ciphertext, knownWord);
// Output the result
printf("%s\n", result);
return 0;
}
3.