Morse Code(Going the other way)

This is a discussion on Morse Code(Going the other way) within the C Programming forums, part of the General Programming Boards category; I have wrote the programming code that converts english to morse, but how do i go back the other way. ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    33

    Morse Code(Going the other way)

    I have wrote the programming code that converts english to morse, but how do i go back the other way. This function does not read or write to files.

    Code:
    void morse_code_letters(char morse_strings[91][6])
    {
    	//morse code letters using string copy
    	strcpy (morse_strings['A'], ".-");
    	strcpy (morse_strings['B'], "-...");
    	strcpy (morse_strings['C'], " -.-.");
    	strcpy (morse_strings['D'], "-..  ");
    	strcpy (morse_strings['E'], ".");
    	strcpy (morse_strings['F'], "..-.");
    	strcpy (morse_strings['G'], "--.");
    	strcpy (morse_strings['H'], "....");
    	strcpy (morse_strings['I'], "..");
    	strcpy (morse_strings['J'], ".---");
    	strcpy (morse_strings['K'], "-.-");
    	strcpy (morse_strings['L'], ".-..");
    	strcpy (morse_strings['M'], "--");
    	strcpy (morse_strings['N'], "-.");
    	strcpy (morse_strings['O'], "---");
    	strcpy (morse_strings['P'], ".--.");
    	strcpy (morse_strings['Q'], "--.-");
    	strcpy (morse_strings['R'], ".-.");
    	strcpy (morse_strings['S'], "...");
    	strcpy (morse_strings['T'], "-");
    	strcpy (morse_strings['U'], "..-");
    	strcpy (morse_strings['V'], "...-");
    	strcpy (morse_strings['W'], ".--");
    	strcpy (morse_strings['X'], "-..-");
    	strcpy (morse_strings['Y'], "-.--");
    	strcpy (morse_strings['Z'], "--..");
    	// morse code numbers using string copy
    	strcpy (morse_strings['1'], ".----");
    	strcpy (morse_strings['2'], "..---");
    	strcpy (morse_strings['3'], "...--");
    	strcpy (morse_strings['4'], "....-");
    	strcpy (morse_strings['5'], ".....");
    	strcpy (morse_strings['6'], "-....");
    	strcpy (morse_strings['7'], "--...");
    	strcpy (morse_strings['8'], "---..");
    	strcpy (morse_strings['9'], "----.");
    	strcpy (morse_strings['0'], "-----");
    	// morse code characters using string copy
    	strcpy (morse_strings['.'], ".-.-.-");
    	strcpy (morse_strings[','], "--..--");
    	strcpy (morse_strings['?'], "..--..");
    	strcpy (morse_strings[' '], " ");
    }


    Code:
    char* letter_to_morse(char *morseTester1, char morse_strings[91][6])
    {
    	int len = strlen(morseTester1);
    	char *result = malloc(len * 6 + 1);
        int i;
    
    	*result = NULL;
    	for (i = 0; i < len; i++)
    	{
    		strcat(result, morse_strings[toupper( morseTester1[i++])]);
    	}
    	return result;
    }
    NOTE: char *morseTester1 = "Hello world"





    I need to know how to convert from morse to english


    Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I would think you would want to be more careful with your spacing than you are.

    Otherwise, just go the other way -- figure out where your morse character starts and stops (hint: you may want to be more careful with your spacing than you are), compare that with your morse characters (hint: you use strcmp to compare strings) and there you go. Since there's only three dozen characters, a linear search would not be horrible, so you can just loop through your characters until you find one.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    	for (i = 0; i < len; i++)
    	{
    		strcat(result, morse_strings[toupper( morseTester1[i++])]);
    	}
    You are incrementing twice per loop iteration.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    33
    Thanks, So would i use a while loop to do that. And i am confused on like where to start and what variables to compare?

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    You have 91 elements in your array, so you can map ASCII codes to morse code strings, but you need to make sure you initialize all 91 elements to something, to avoid garbage strings. Then, use either a while or do loop to scan through morse_strings, and a strcmp to check the value in question against the value in morse_strings[i]. If they match, return i, which will map directly to a valid ASCII character that should be suitable for printing to screen.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    I dont know why you need an array with 91 elements, when your not using all of the elements you declared. You could do the following to just reduce the whole lot down to a resonable size

    Code:
    char morse_strings[41][6];
    .
    .
    strcpy (morse_strings['0'-'0'], ".-");
    strcpy (morse_strings['1'-'0'], "-...");
    strcpy (morse_strings['2'-'0'], " -.-.");
    .
    .
    strcpy (morse_strings['Z'-'0'], " -.-.");
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ssharish2005 View Post
    I dont know why you need an array with 91 elements, when your not using all of the elements you declared. You could do the following to just reduce the whole lot down to a resonable size

    Code:
    char morse_strings[41][6];
    .
    .
    strcpy (morse_strings['0'-'0'], ".-");
    strcpy (morse_strings['1'-'0'], "-...");
    strcpy (morse_strings['2'-'0'], " -.-.");
    .
    .
    strcpy (morse_strings['Z'-'0'], " -.-.");
    ssharish
    Probably because his way produces direct morse to ascii and ascii to morse conversion based on the array index.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ssharish2005 View Post
    I dont know why you need an array with 91 elements, when your not using all of the elements you declared. You could do the following to just reduce the whole lot down to a resonable size

    Code:
    char morse_strings[41][6];
    .
    .
    strcpy (morse_strings['0'-'0'], ".-");
    strcpy (morse_strings['1'-'0'], "-...");
    strcpy (morse_strings['2'-'0'], " -.-.");
    .
    .
    strcpy (morse_strings['Z'-'0'], " -.-.");
    ssharish
    That would make it a bit difficult to use comma and period.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    33
    Code:
    char* morse_to_letter(char *morseTester2, char morse_strings[91][6])
    {
    	int i = 0, count = 0;
    	char morse[500 + 1];
    
    	while (strcmp(morse, morse_strings[i]) == 0)
    	{
    		return i;
    		count++;
    	}
    
    	return i;
    }
    do u have any suggestions? it returns NULL

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    33
    and ya its because of the ASCII value

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How do you know when you are at the end of a letter? Why is .- A instead of ET?


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And where's the data you intend to check?

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    33
    thats what its supposed to be i believe

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    Quzah brings up a good point. When one transmits morse code, there is often a slight pause between letters and a longer pause between words. In your code, perhaps you want to represent this by putting a single space between each letter, and perhaps 2 or 3 spaces between words.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ummm... you need to know what your counting...

    Code:
    {
    	int i = 0
    	while (strcmp(morse, morse_strings[i++]) != 0 && i < 96);
    	return i-1;
    }
    Using this in a function you need to somehow pre-divide the longer morse string into letters and feed the function individual letters... so that it can evaluate each morse string separately.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. morse code convertor
    By neandrake in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:53 AM
  2. Morse code conversion
    By viclaster in forum C Programming
    Replies: 6
    Last Post: 12-04-2003, 08:24 PM
  3. Morse Code Translator!
    By Paro in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2002, 06:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21