GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Nov-2005, 06:27
fadedg2 fadedg2 is offline
Junior Member
 
Join Date: Sep 2005
Posts: 35
fadedg2 is on a distinguished road

morse to english now


hello 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  
Old 03-Nov-2005, 10:17
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: morse to english now


Hi 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:
#define MAX_STR_SIZE 80
#include <stdio.h>
#include <string.h>


static char morsecode[45][8] = {
      "a.-","b-...","c-.-.","d-..","e.","f..-.","g--.","h....","i..",
      "j.---","k-.-","l.-..","m--","n-.","o---","p.--.","q--.-","r.-.",
      "s...","t-","u..-","v...-","w.--","x-..-","y-.--","z--..","0-----",
      "1.----","2..---","3...--","4....-","5.....","6-....","7--...",
      "8---..","9----.","..-.-.-",",--..--",":---...","?..--..","'.----.",
      "--....-","/-..-.","(-.--.-","\.-..-."};
      
int main()                                        /*main must return an int*/                        
{
      char me[] = "..-. .- -.. . -.. --. ..---";  /*a character array*/
      char *ptr;                                  /*ptr holds the value of one morse word*/    
      int i;
      
      ptr = strtok (me," ");                      /*get one morse word and store it in ptr*/

      while (ptr != NULL)                         /*check whether ptr is a valid string*/
      { 
        
         for(i = 0; i < 45; i  ++)                /*check all the 45 morse words*/
         {
               
            
               if(strstr(morsecode[i],ptr))   /*check whether there is a substring ptr in morsecode array*/
               {
                     printf("%c",morsecode[i][0]);  /*print the first character of the morsecode array*/
                     goto next;                     /*go to the next value*/
               }
                   
         }
         
         next:
         ptr = strtok (NULL, " ");                  /*get the next morse word*/
      }
      
      getchar();                                    /*pause the program for a while*/
      return 0;                                     /*main must return 0*/      
}


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  
Old 03-Nov-2005, 10:45
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: morse to english now


Quote:
Originally Posted by Paramesh
I can post the full code, but that means that you do not think.

I agree with you, Paramesh (who should be studying right now for his finals ... actually so am I )
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  
Old 03-Nov-2005, 13:03
fadedg2 fadedg2 is offline
Junior Member
 
Join Date: Sep 2005
Posts: 35
fadedg2 is on a distinguished road

Re: morse to english now


i 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  
Old 03-Nov-2005, 14:04
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: morse to english now


Quote:
Originally Posted by fadedg2
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.

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:
char morse_to_character(char morse_code_characte[MORSE_MAX_LENGTH]);

Now, since you need to translate a whole string into english, you will also need the function :

CPP / C++ / C Code:
char *morse_to_english(char morse_code_line[MORSE_MAX_LINE_LENGTH]);

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  
Old 03-Nov-2005, 14:20
fadedg2 fadedg2 is offline
Junior Member
 
Join Date: Sep 2005
Posts: 35
fadedg2 is on a distinguished road

Re: morse to english now


i 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  
Old 03-Nov-2005, 16:11
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: morse to english now


Hey 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  
Old 04-Nov-2005, 10:05
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: morse to english now


Quote:
Originally Posted by Paramesh
Hey Greg.

2. Then, it will search for the particular morse word in the morsecode array.
This is how it does:


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?

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:
morsecode[i][0] is the char for the ith entry &morsecode[i][1] is the address of the first element in the null-terminated sequence of chars that is the morse string for the ith entry. I even printed out the char and the morse string for each table entry.



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:

int morse_to_char(char *str)
{
  int i;
  int retvalue = 0;

  for (i = 0; i < NUM_TABLE_ENTRIES; i++) {
    if (strcmp(&morsecode[i][1], str) == 0) {
        retvalue = morsecode[i][0];
        break;
    }
  }
  return retvalue;
}

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  
Old 04-Nov-2005, 10:15
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: morse to english now


Thank you Dave.

I was keeping in mind this code:
CPP / C++ / C Code:
      char me[] = "..-. .- -.. . -.. --. ..---";  /*a character array*/
      char *ptr;                                  /*ptr holds the value of one morse word*/    
      char *temp;                                 /*temp is a temporary variable*/
      int i;
      
      ptr = strtok (me," ");                      /*get one morse word and store it in ptr*/

      while (ptr != NULL)                         /*check whether ptr is a valid string*/
      { 
        
         for(i = 0; i < 45; i  ++)                /*check all the 45 morse words*/
         {
               
             temp = strstr(morsecode[i],ptr);   /*check whether ptr occurs in morsecode array and store it in temp*/ 
              
               if(temp != NULL && strcmp(temp,ptr) == 0)   /*check whether temp and ptr are same and whether temp is valid*/
               {
                     printf("%c",morsecode[i][0]);  /*print the first character of the morsecode array*/
                     goto next;                     /*go to the next value*/
               }
                   
         }
         
         next:
         ptr = strtok (NULL, " ");                  /*get the next morse word*/
      }

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  
Old 22-Dec-2005, 07:58
hello2001 hello2001 is offline
New Member
 
Join Date: Dec 2005
Posts: 7
hello2001 is on a distinguished road

Re: morse to english now


where u used char me[] = "..-. .- -.. . -.. --. ..---";

can u ask user to type in
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
english to morse revisited fadedg2 C Programming Language 16 10-Nov-2006 21:29
need help converting between morse and english fadedg2 C Programming Language 2 29-Oct-2005 11:02
D3D error when running fullscreen games/programs - help?! daa709 Computer Hardware Forum 4 01-Jul-2005 09:03
Morse Code for C sibyl03 C Programming Language 1 13-Oct-2004 01:00
new russian game in english !!! Hellraiser Computer Software Forum - Games 1 16-Sep-2004 01:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:15.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.