I am trying to replace characters in a string for characters stored in some arrays:
char encode_table[122];
char decode_table[122];
...
int main()
{
memset(encode_table, 0, 122);
memset(decode_table, 0, 122);
...
To fill in the table, I use a file in the format
a b
c d
where the mappings to b, c are mapped to d, etc. I store the mappings in an array using the ASCII values โโof the mapped characters as indices.
encode_table[97] // Asking for the mapping of 'a'. Returns 'b'
After matching all the characters, I parse the file line by line. Each line is processed by another function, which is supposed to replace the characters that need to be replaced, and leave the rest alone.
void display(char * filename){
char buffer[255];
FILE * file = fopen(filename, "r");
...
while(fgets(buffer, sizeof(buffer), file){
display_line(buffer);
}
}
void display_line(char * line){
char c;
char c_r;
char format_str[255];
if(encode || decode){
for(int i = 0; i < strlen(line); i++){
c = line[i];
c_r = (encode ? encode_table[(int)c] : decode_table[(int)c]);
if((int)c != O){
if(c == EOF){
break;
}
if((int)c_r != 0){
format_str[strlen(format_str)] = c_r;
}
else{
format_str[strlen(format_str)] = c;
}
}
}
printf("%s", format_str);
memset(format_str, 0, strlen(format_str));
}
As far as I can tell, the encode_table and decode_table are built correctly (in this example, I only map the English characters to other characters in the English alphabet. The mapping is 1 to 1):
encode_table:
{0: , 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: Z, 66: Y, 67: X, 68: W, 69: V, 70: U, 71: T, 72: S, 73: R, 74: Q, 75: P, 76: O, 77: N, 78: M, 79: L, 80: K, 81: J, 82: I, 83: H, 84: G, 85: F, 86: E, 87: D, 88: C, 89: B, 90: A, 91: , 92: , 93: , 94: , 95: , 96: , 97: z, 98: y, 99: x, 100: w, 101: v, 102: u, 103: t, 104: s, 105: r, 106: q, 107: p, 108: o, 109: n, 110: m, 111: l, 112: k, 113: j, 114: i, 115: h, 116: g, 117: f, 118: e, 119: d, 120: c, 121: b, 122: a, }
decode_table:
{0: , 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: Z, 66: Y, 67: X, 68: W, 69: V, 70: U, 71: T, 72: S, 73: R, 74: Q, 75: P, 76: O, 77: N, 78: M, 79: L, 80: K, 81: J, 82: I, 83: H, 84: G, 85: F, 86: E, 87: D, 88: C, 89: B, 90: A, 91: , 92: , 93: , 94: , 95: , 96: , 97: z, 98: y, 99: x, 100: w, 101: v, 102: u, 103: t, 104: s, 105: r, 106: q, 107: p, 108: o, 109: n, 110: m, 111: l, 112: k, 113: j, 114: i, 115: h, 116: g, 117: f, 118: e, 119: d, 120: c, 121: b, 122: a, }
( ), , , , ,
:
For some reason, this program will not work.
:
Uli hln?I?Uv iv4M1zhlm?I?U, gs4M1rh k?I?Uilti??%1zn droo mlg dlip.
( "Uli hln" - " som" , (? I? U), (v is e ) ..
. ?