![]() |
|
#1
|
|||
|
|||
need help converting between morse and englishi am having a terrible time trying to convert from morse code to english and from english to morse code. i am given a conversion array as shown below...
CPP / C++ / C Code:
where the dashes and lines are the morse code and the letters are the english. if i am trying to covert a string with dashes and dots to its english counter part and vice versa what should i do? any suggestions? i have tried using strstr and strchr but am not succeeding. ive also tried using array operations. these ways also seem very tedious. Does any body have suggestions on an easier or less tedious way?Or does anyone even have any suggestions at all? thank you in advance and hope you can shed me some light on this. |
|
#2
|
|||
|
|||
Re: need help converting between morse and englishQuote:
You are given enough to get started, but let's work our way through a few things first. A two-dimensional array of chars can be thought of as an "array of (array of chars)" What do I mean by that? Well given morsecode[45][8], let's look at some notation: morsecode[0][0] is a char, morsecode[0][1] is a char, ... morsecode[0][7] is a char. In other words, and in C notation, morsecode[0] can be treated as an array[8] of char. Simirlarly, morsecode[1] is an array[8] of char, etc. Now look at the initialization: This is the same as if it had been written: CPP / C++ / C Code:
Heck, lets just write a program to tell us what morsecode[0] is: CPP / C++ / C Code:
Note that the char '\0' is equal to an integer value of zero, not the ASCII char '0', and not something that can be printed with %c. My output: Code:
Now, if we look at memory beginning at morseCode[0][1], we see a null-terminated sequence of chars. The C language doesn't have a "string" data type, but it does have library functions that treat null terminated sequences of chars as "strings". The functions all take arguments that are of type "pointer to char". The null-terminated sequence of chars beginning at morseCode[0][1] is the morse "string" for the char in morseCode[0]. So, try adding the following two lines to the program (just before the return 0;). CPP / C++ / C Code:
See: the value of term &morseCode[i][1] is a pointer to the location in memory of morsecode[i][1], which is exactly what the "%s" field of the printf() statement needs! My output now is: [code] morseCode[0][0] = 'a' morseCode[0][1] = '.' morseCode[0][2] = '-' morseCode[0][3] = '\0' morseCode[0][4] = '\0' morseCode[0][5] = '\0' morseCode[0][6] = '\0' morseCode[0][7] = '\0' The ASCII char for morsecode[0] is 'a' The Morse string for morseCode[0] is ".-" [/c] (If you are interested, try it for different values of i.) Now, we are ready for some code: each table entry has an ascii char and its morse "string" of dits and das. Now I suggest you write two functions: 1. A function that takes a char and looks up its morse string from the table: You simply make a loop (let i go from 0 to 44) that sees if morsecode[i] is equal to the char, If it is, then return a pointer to morsecode[i][1]. The morse table is global (this is one of those places where it is really appropriate to use a global variable, so you can simply return the address of morsecode[i][1]. (If the user gives you a char that isn't in the table, you could return a NULL pointer. CPP / C++ / C Code:
2. Write a function that takes a "morse string" and looks to see if it is in the table. How? make a loop (let i go from 0 to 44) For each i do a strcmp() with the morse string of ith entry in the table. (strcmp(msg, &morsecode[i][1]). return the value of I that gave a match. If the user gave you a "string" that is not in the table, maybe you could return, say, -1. CPP / C++ / C Code:
After you have tested these functions (how?), then you can make a program takes an ascii string and converts to morse chars using your asciiToMorse() function. Finish this part first. Make sure it works exactly as you expect. Be sure to test it with illegal chars as well as the ones in the table. After testing this, then make a program that takes a sequence of morse strings and converts to ASCII, using your morseToAscii() function. Regards, Dave [InternationalMorse]-.. .- ...- . -.- .-- --... -..-[/InternationalMorse] |
|
#3
|
|||
|
|||
Re: need help converting between morse and englishQuote:
Correction: it should be: You simply make a loop (let i go from 0 to 44) that sees if morsecode[i][0] is equal to the char, [InternationalMorse]... --- .-. .-. -.--[/InternationalMorse] Regards, Dave |
Recent GIDBlog
Meeting the populace by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Morse Code for C | sibyl03 | C Programming Language | 1 | 13-Oct-2004 00:00 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The