c - Replacing some characters in a string with characters stored in an array inserts garbage -


i have been trying replace characters in string characters stored in arrays:

char encode_table[122]; char decode_table[122];  ...  int main() {     memset(encode_table, 0, 122);     memset(decode_table, 0, 122);     ... 

to populate table, use file in format

a b c d 

where maps b, c maps d, etc. store mappings in array, using indices ascii values of mapped characters.

encode_table[97] // asking mapping of 'a'. returns 'b' 

after map characters, parse file, line line. each line processed function supposed replace characters must replaced , leaves alone rest.

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 = 0; < strlen(line); i++){             c = line[i];             c_r = (encode ? encode_table[(int)c] : decode_table[(int)c]);              if((int)c != o){  // don't print empty chars in buffer                 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)); // reset char array next iteration } 

as far can tell, encode_table , decode_table built (in example, i'm mapping english-alphabet characters other english-alphabet characters. mapping 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, } 

when try running program on text file, (most of) characters seem map correct mapping, there lot of garbage, in between characters in original text file

original:

for reason, program not work. 

program output:

uli hln?i?uv iv4m1zhlm?i?u, gs4m1rh k?i?uilti??%1zn droo mlg dlip. 

most characters seem correctly mapped ('uli hln' 'for som' in original file, comes bunch of garbage (?i?u), , continues mapping (v e in original), , forth.

i've been staring @ couple of hours. ideas?

your code relies on strlen(format_str) set zeros initially. however, there no memset that. how random "garbage" characters end among characters of "good" output.

although memset(format_str, 0, sizeof(format_str)) fix problem, adding pointer or index writing better:

int j = 0; ... format_str[j++] = c_r; ... // after loop over, null-terminate string: format_str[j++] = '\0'; 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -