GIDForums  

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

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Oct-2005, 05:36
fadedg2 fadedg2 is offline
Junior Member
 
Join Date: Sep 2005
Posts: 35
fadedg2 is on a distinguished road

need help converting between morse and english


i 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:

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----.","..-.-.-",",--..--",":---...","?..--..","'.----.",
      "--....-","/-..-.","(-.--.-","\".-..-."};


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  
Old 29-Oct-2005, 08:00
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
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: need help converting between morse and english


Quote:
Originally Posted by fadedg2
i am given a conversion array as shown below...



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:
morsecode[0][0] = 'a';
morsecode[0][1] = '.';
morsecode[0][2] = '-';
morsecode[0][3] = '\0'; /* note: '\0' is equal to binary zero, not ascii '0' */
...
morsecode[0][7] = '\0';

Heck, lets just write a program to tell us what morsecode[0] is:

CPP / C++ / C Code:
#include <stdio.h>

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()
{
  int i, j;
  i = 0;
  for (j = 0; j < 8; j++) {
    printf("morseCode[%d][%d] = ", i, j);
    if (morseCode[i][j] == 0) {
      printf("'\\0'");
    }
    else {
      printf("'%c'", morseCode[i][j]);
    }
    printf("\n");
  }
  return 0;
}

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:
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'

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:
  printf("The ascii char for morsecode[%d] is '%c'\n", i, morseCode[i][0]);
  printf("The Morse string for morseCode[%d] is \"%s\"\n", i, &morseCode[i][1]);

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:
char *asciiToMorse(char c)
{
}

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:
char *morseToAscii(char *m)
{
}

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  
Old 29-Oct-2005, 10:02
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
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: need help converting between morse and english


Quote:
Originally Posted by davekw7x

You simply make a loop (let i go from 0 to 44) that sees if morsecode[i] is equal to the char,

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 GIDBlogMeeting the populace by crystalattice

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
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

All times are GMT -6. The time now is 17:08.


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