-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleEncode.c
More file actions
78 lines (62 loc) · 2.23 KB
/
simpleEncode.c
File metadata and controls
78 lines (62 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* simpleEncode.c
*
* reads in a permutation of the alphabet then encodes
* lower case letters using that permutation
* module 4 template code you asked for - might need some cleaning up...
*
* Created by Julian Saknussemm on 11/09/1752
* Licensed under Creative Commons BY 3.0.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define STOP -1
#define ALPHABET_SIZE 26
#define ALPHABET "abcdefghijklmnopqrstuvwxyz"
char encode (int plainChar, char *permutation);
void testEncode (void);
int main (int argc, char *argv[]) {
testEncode();
char permutation[ALPHABET_SIZE + 1];
scanf ("%s", permutation);
// getchar() reads and returns one character from the keyboard
// returns -1 when the input is finished / end-of-file is reached
// signal this from the kayboard by pressing ^D in linux/^Z windows
int plainCharacter = getchar();
while (plainCharacter != STOP) {
int encodedCharacter = encode (plainCharacter, permutation);
printf ("%c", encodedCharacter);
plainCharacter = getchar();
}
return EXIT_SUCCESS;
}
void testEncode (void) {
assert (encode ('A',"abcdefghijklmnopqrstuvwxyz") == 'A');
assert (encode ('?',"abcdefghijklmnopqrstuvwxyz") == '?');
assert (encode (' ',"abcdefghijklmnopqrstuvwxyz") == ' ');
assert (encode ('\n',"abcdefghijklmnopqrstuvwxyz") == '\n');
assert (encode ('a',"abcdefghijklmnopqrstuvwxyz") == 'a');
assert (encode ('m',"abcdefghijklmnopqrstuvwxyz") == 'm');
assert (encode ('z',"abcdefghijklmnopqrstuvwxyz") == 'z');
assert (encode ('a',"bcdefghijklmnopqrstuvwxyza") == 'b');
assert (encode ('m',"bcdefghijklmnopqrstuvwxyza") == 'n');
assert (encode ('z',"bcdefghijklmnopqrstuvwxyza") == 'a');
assert (encode ('a',"qwertyuiopasdfghjklzxcvbnm") == 'q');
assert (encode ('b',"qwertyuiopasdfghjklzxcvbnm") == 'w');
assert (encode ('z',"qwertyuiopasdfghjklzxcvbnm") == 'm');
}
char encode (int plainChar, char *permutation) {
char *alphabet = ALPHABET;
char encodedChar = plainChar;
int marker = 0;
while (marker < ALPHABET_SIZE) {
if(plainChar == alphabet[marker]) {
encodedChar = permutation[marker];
}
marker++;
}
return encodedChar;
}