c - Trying to convert morse code to english. struggling -
i'm trying create function read morse code 1 file, convert english text, print converted text terminal, , write output file. here's rough start...
#define total_morse 91
#define morse_len 6
void morse_to_english(file* inputfile, file* outputfile, char morsestrings[total_morse][morse_len]) { int = 0, compare = 0; char convert[morse_len] = {'\0'}, *buffer = '\0'; //read in line of morse string file // fgets(buffer, //then what? while(((convert[i] = fgetc(inputfile)) != ' ') && (i < (morse_len - 1))) { i++; } if (convert[i + 1] == ' ') convert[i + 1] = '\0'; //compare read-in string w/morsestrings (i = 48, compare = strcmp(convert, morsestrings[i]); //48 '0' < (total_morse - 1) && compare != 0; i++) { compare = strcmp(convert, morsestrings[i]); } printf("%c", (char)i); }
i have initialized morsestrings morse code. that's function right now. not work, , i'm not sure approach take.
my original algorithm plan this:
1. scan morse code in file, character character, until space reached
1.1 save temporary buffer (convert)
2. loop while < 91 && compare != 0
compare = strcmp(convert, morsestring[i])
3. if (test ==0) print ("%c", i);
4. loop through until eof
but.. can't seem think of way test if next char in file space. has made difficult me.
i got pretty frustrated , googled ideas, , found suggestion use algorithm
- read line
loop
-strchr() space or eol -copy characters before space string
-use strcmp() , loop find letter -test next character space.
-if so, output space -skip next morse characterlist item
endloop
but, loops kind of confusing. use fgets() (i think), don't know put in length argument.
anyways, i'm tired , frustrated. appreciate or insight problem. can provide more code if necessary.
your original plan looks fine. you're off 1 when check ' '
in buffer, though. it's @ convert[i]
, not convert[i + 1]
. i++
inside loop doesn't happen when space detected.
Comments
Post a Comment