Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define MAP_SIZE 26
- void createRandomMap(char map[MAP_SIZE]) {
- int i;
- for (i = 0; i < MAP_SIZE; i++) {
- map[i] = 'A' + i;
- }
- // Fisher - Yates algorithm
- for (i = MAP_SIZE - 1; i > 0; i--) {
- int j = rand() % (i + 1);
- char temp = map[i];
- map[i] = map[j];
- map[j] = temp;
- }
- }
- void applyMapping(char *j, int count, char charMap[MAP_SIZE], int reverse) {
- int i;
- for (i = 0; i < count; i++) {
- if (j[i] >= 'A' && j[i] <= 'Z') {
- if (reverse) {
- char mappedChar = 'A';
- while (charMap[mappedChar - 'A'] != j[i]) {
- mappedChar++;
- }
- j[i] = mappedChar;
- } else {
- j[i] = charMap[j[i] - 'A'];
- }
- }
- }
- }
- int main() {
- srand(time(NULL));
- char charMap[MAP_SIZE];
- createRandomMap(charMap);
- char *j = malloc(sizeof(char));
- char input;
- int count;
- int reverseMapping = 0;
- while (1) {
- printf("Enter capital letters (terminate with '$', or 'q' to quit): ");
- count = 0;
- while (1) {
- scanf(" %c", &input);
- if (input == '$') {
- break;
- } else if (input == 'q') {
- free(j);
- return 0;
- }
- if (input >= 'A' && input <= 'Z') {
- j = realloc(j, (count + 1) * sizeof(char));
- j[count] = input;
- count++;
- }
- }
- printf("Original characters:\n");
- int i;
- for (i = 0; i < count; i++) {
- printf("%c", j[i]);
- }
- printf("\n");
- applyMapping(j, count, charMap, reverseMapping);
- printf("Mapped characters:\n");
- for (i = 0; i < count; i++) {
- printf("%c", j[i]);
- }
- printf("\n\n");
- reverseMapping = !reverseMapping;
- }
- free(j);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment