![]() |
|
#1
|
|||
|
|||
morse to english nowhello all. well i greatly appreciate those who gave me a helping hand on the previus morsecode thread. i have not been to bed all night. i have been up all night working on this program. i am obviously a beginner so bare with me.
i now need a quick and simple way to convert morse to english. i need to write a C function that will convert a morsecode string(having 1 blank between each coded letter) into english (with no blanks between words). it should produce all lowercase letters. i am sorry to keep drilling these questions here but the program was due yesterday, so you can see how i am pressed for time. just so you know, i DID NOT procrastinate, i know it seems like it but i have been working on this for about a week now and obviously have not been having much luck. Please help me. |
|||
|
#2
|
||||
|
||||
Re: morse to english nowHi fadedg2,
I have written a sample program for this: Here is the description: 1. I used the standard string library for this. 2. First, we need to split the morse code by using the spaces between them. 3. So, for this, i used the strtok function. You can visit this thread for more description on how to split strings. 4. After we split the strings, we need to search for that particular sequence. 5. For that, i used the strstr function. 6. Using strstr, i found the location of the morse code. Here is the sample code: CPP / C++ / C Code:
Note that this code does not work fully. instead of displaying fadedg2, it will display fababg2. So, you have to think why it does like this, and what should you do to solve this problem. I can post the full code, but that means that you do not think. You will not understand the program fully. SO, what you have to do now is : 1. Understand the program fully. 2. tell us why it didnt display the correct answer. Hint for solving the problem: Use a temporary variable and the strcmp function. Cheers, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#3
|
||||
|
||||
Re: morse to english nowQuote:
I agree with you, Paramesh (who should be studying right now for his finals ... Nevertheless, fadedg2, I did post a function that translates morse to english in your previous thread (which, by the way, could simply be continued - why the extra thread ? your questions are related You could, with minor modifications (you want lower case letters) and Paramesh's example, use this function ... Best regards, Kobi Hikri. __________________
It's actually a one time thing (it just happens alot). |
|
#4
|
|||
|
|||
Re: morse to english nowi am going through the code but am not understanding why it is not returning the correct characters. i have made some modifications, but none of those seem to help. im not sure about using the strcmp. can you point me a little closer please.
|
|
#5
|
||||
|
||||
Re: morse to english nowQuote:
Many times, in many problems (not only programming problems) the answer is derived directly from the problem definition. Now, in order to translate a line written in morse code, you need to translate a single morse code into character, right ? This will be your first function : CPP / C++ / C Code:
Now, since you need to translate a whole string into english, you will also need the function : CPP / C++ / C Code:
This function will go token by token (as Paramesh showed you) and translate character after character in each token into english. It will use the first function. This is how I would keep it simple, I hope I helped. Kobi. __________________
It's actually a one time thing (it just happens alot). |
|
#6
|
|||
|
|||
Re: morse to english nowi dont understand why parameshes code does not give the correct output. Is it really that obvious why it doesnt work correctly? Or is it trickier than that?
|
|
#7
|
||||
|
||||
Re: morse to english nowHey Greg.
You are very stubborn. I like that. - Repeating what Kobi said. So, why it does not give the correct answer? You will know the answer once you understand what is strstr function. It is a little bit trickier. So, what the program does after all? 1. It first splits the me string into separate morsewords. So, in that example, i used char me[] = "..-. .- -.. . -.. --. ..---"; Hence, it first separates ..-. from the me string and stores it in ptr. 2. Then, it will search for the particular morse word in the morsecode array. This is how it does: 3. It goes to the first morsecode word. i.e "a.-" 4. It checks whether the text ..-. is there in the first array. i.e morsecode[i]. 5. Since it cant find the pattern, it goes to the next morsecode[i]. It will continue till it finds a correct pattern. 6. When it goes to "f..-.", it finds a match for the string ptr. 7. So, it displays the first character of the morsecode[i] string by morsecode[i][0]. 8. After displaying the character, it goes to the next pattern in the me[] string using the statement ptr = strtok (NULL, " "); . Now, ptr will contain ".-". 9. it searched for the corresponding pattern. it finds one in the first array itself. so, it goes for the next morse pattern. now, ptr will contain "-.." 10. Here is the trickier part. It will search for "-.." pattern in the first morsecode array. i.e "a.-",. There is no match. it goes to the next one. "b-..." . Here comes the problem. the pattern "-.." is present in this string!!!!! Actually we want d to be displayed because it is "d-..". But since it finds the pattern included in the string "b-...", it will display b and goes to the next pattern. Can you understand the problem here? Similarly, it will search or "." i.e 'e'. But the "." is already available in "a.-" So, it will display a instead of e. I hope you understand this bit. So, what is the problem now? We cannot simply display the character if we find a match for it. We should find the best possible match. or the exact match i.e if we want to search "." then "." is available in almost all of the morse code. But the exact match is "e." How we are going to solve the problem? By using a temporary string and the strcmp function. the strcmp function is used to compare two strings. Here is the logic for solving the problem: store the strstr(morsecode[i],ptr) in the temp variable. Then using strcmp function, compare it with the ptr string. If they both are equal, we have found the exact match and we can display the first character. Else, there is not an exact match of temp and ptr. so go to the next variable in the loop. Note that temp can also be NULL. You should also check for that. Can you understand this bit? Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#8
|
|||
|
|||
Re: morse to english nowQuote:
So: why use strstr()????? Why not just use strcmp()? As I indicated in a post several threads ago, for integer i (i greater than or equal to zero and i less than the number of table entries): Code:
So the loop looking for the morse string, str, is something like the following (assuming that NUM_TABLE_ENTRIES is a #defined constant, and morsecode[NUM_TABLE_ENTRIES][] is a global variable. CPP / C++ / C Code:
Note that if it didn't find the morse string in the table, it returns the value 0. If it did find the morse string in the table, it returns that string's char. Regards, Dave |
|
#9
|
||||
|
||||
Re: morse to english nowThank you Dave.
I was keeping in mind this code: CPP / C++ / C Code:
But now i understand your code and it is the better one. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#10
|
|||
|
|||
Re: morse to english nowwhere u used char me[] = "..-. .- -.. . -.. --. ..---";
can u ask user to type in |
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| english to morse revisited | fadedg2 | C Programming Language | 16 | 10-Nov-2006 20:29 |
| need help converting between morse and english | fadedg2 | C Programming Language | 2 | 29-Oct-2005 10:02 |
| D3D error when running fullscreen games/programs - help?! | daa709 | Computer Hardware Forum | 4 | 01-Jul-2005 08:03 |
| Morse Code for C | sibyl03 | C Programming Language | 1 | 13-Oct-2004 00:00 |
| new russian game in english !!! | Hellraiser | Computer Software Forum - Games | 1 | 16-Sep-2004 00:52 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The