Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

URGENT School program - morse code convert to/from alpha/digital

Status
Not open for further replies.

Row

Technical User
Feb 21, 2000
4
US
We have been assigned to do a project from Deitel/Deitel's &quot;C How to Program&quot; 2nd Edition pg 361, #8.39. I have been working this for about 8 hours and just cannot get it to work. I have put my answer below and hope someone can look at it and see what is wrong. I just cann't get it. Feel free to email me at <A HREF="mailto:Rowlandl@aol.com">Rowlandl@aol.com</A>, hope to hear from someone soon<br>
<br>
#include &lt;stdio.h&gt;<br>
#include &lt;string.h&gt;<br>
#include &lt;ctype.h&gt;<br>
<br>
/* Put spaces at end of morse code string to simplify processing. */<br>
char *MorseAlphabet[26] = {<br>
&quot;.- &quot;, /* A */<br>
&quot;-... &quot;, /* B */<br>
&quot;-.-. &quot;, /* C */<br>
&quot;-.. &quot;, /* D */<br>
&quot;. &quot;, /* E */<br>
&quot;..-. &quot;, /* F */<br>
&quot;--. &quot; , /* G */<br>
&quot;.... &quot;, /* H */<br>
&quot;.. &quot; , /* I */<br>
&quot;.--- &quot;, /* J */<br>
&quot;-.- &quot;, /* K */<br>
&quot;.-.. &quot;, /* L */<br>
&quot;-- &quot;, /* M */<br>
&quot;-. &quot;, /* N */<br>
&quot;--- &quot;, /* O */<br>
&quot;.--.&quot;, /* P */<br>
&quot;--.- &quot;, /* Q */<br>
&quot;.-. &quot;, /* R */<br>
&quot;... &quot;, /* S */<br>
&quot;- &quot;, /* T */<br>
&quot;..- &quot;, /* U */<br>
&quot;...- &quot;, /* V */<br>
&quot;.-- &quot;, /* W */<br>
&quot;-..- &quot;, /* X */<br>
&quot;-.--&quot;, /* Y */<br>
&quot;--.. &quot;, /* Z */<br>
};<br>
<br>
char *MorseDigits[10] = {<br>
&quot;----- &quot;, /* 0 */<br>
&quot;.---- &quot;, /* 1 */<br>
&quot;..--- &quot;, /* 2 */<br>
&quot;...--&quot; , /* 3 */<br>
&quot;....- &quot;, /* 4 */<br>
&quot;..... &quot;, /* 5 */<br>
&quot;-.... &quot;, /* 6 */<br>
&quot;--... &quot;, /* 7 */<br>
&quot;---.. &quot;, /* 8 */<br>
&quot;----. &quot;, /* 9 */<br>
};<br>
<br>
int ConvertToAlpha(char *in, char *out, int size);<br>
<br>
int main()<br>
{<br>
char myTest[] = &quot;..... .... . .-.. .-.. --- ... &quot;;<br>
char result[1024];<br>
int blError;<br>
<br>
printf(&quot;Converting %s\n&quot;, myTest);<br>
blError = ConvertToAlpha(myTest, result, sizeof(result));<br>
if(!blError)<br>
printf(&quot;retrieved: %s\n&quot;, result);<br>
else<br>
printf(&quot;error, got partial result: %s\n&quot;, result);<br>
<br>
return 0;<br>
}<br>
<br>
/*<br>
* FUNCTION: ConvertToAlpha<br>
* PURPOSE: accepts a buffer of morse code and converts it into alphanumeric.<br>
* Returns 1 if successful, 0 if if it fails.<br>
*/<br>
int ConvertToAlpha();<br>
char *in; /* Pointer to input buffer (contains alpha, digit, spaces) */<br>
char *out; /* Pointer to output buffer (will contain morse code) */<br>
int size; /* Size of the output buffer */<br>
<br>
<br>
char *pszCurrIn;<br>
char *pszCurrOut;<br>
int i;<br>
int iIndex;<br>
int blDone = 1;<br>
int iLength;<br>
int blFound;<br>
int blError = 1;<br>
<br>
<br>
/* Initialize the current out and in pointers*/<br>
pszCurrIn = in;<br>
pszCurrOut = out;<br>
<br>
/* Intialize buffer count to 0*/<br>
i = 0;<br>
<br>
/* We do the loop until we have either filled the buffer or we've finished. */<br>
/* Note, a space is reserved for the terminator...*/<br>
while(i &lt; (size - 1) ¦¦ !blDone)<br>
{<br>
/* Is it the beginning of a morse code token?*/<br>
if('\0' != *pszCurrIn && !isspace(*pszCurrIn))<br>
{<br>
/* Yup. Init the found flag to zero*/<br>
blFound = 0;<br>
<br>
/* Is this an alphabetical morse code token?*/<br>
for(iIndex = 0; iIndex &lt; 26 && !blFound; iIndex++)<br>
{<br>
if(!strncmp(*pszCurrIn, MorseAlphabet[iIndex], strlen(MorseAlphabet[iIndex])))<br>
{<br>
/* Apparently so - copy the correct character into the output.*/<br>
sprintf(pszCurrOut, &quot;%c&quot;, iIndex + 'A');<br>
<br>
/* Get the length of the morse code token*/<br>
iLength = strlen(MorseAlphabet[iIndex]);<br>
<br>
/* Increase pszCurrIn by the length of the token (go past it)*/<br>
pszCurrIn += iLength;<br>
<br>
/* Increase outbuffer used count*/<br>
i++;<br>
<br>
/* Signify we found the token match.*/<br>
blFound = 0;<br>
<br>
/* Go to the next location on the buffer output.*/<br>
pszCurrOut++;<br>
}<br>
}<br>
<br>
/* Did we not find the correct buffer token in alphabet land?*/<br>
if(!blFound)<br>
{<br>
/* Nope. Check to see if it is a digit.*/<br>
for(iIndex = 0; iIndex &lt; 10 && !blFound; iIndex++)<br>
{<br>
if(!strncmp(pszCurrIn, MorseDigits[iIndex], strlen(MorseDigits[iIndex])))<br>
{<br>
/* Found it. copy the correct character into the output*/<br>
sprintf(pszCurrOut, &quot;%c&quot;, iIndex + '0');<br>
<br>
/* Get the length of the morse code token*/<br>
iLength = strlen(MorseDigits[iIndex]);<br>
<br>
/* Increase pszCurrIn by the length of the token (go past it)*/<br>
pszCurrIn += iLength;<br>
<br>
/* Increase outbuffer used count.*/<br>
i++;<br>
<br>
/* Signify we found a token match.*/<br>
blFound = 1;<br>
<br>
/* Go to the next location on the buffer output.*/<br>
pszCurrOut++;<br>
}<br>
}<br>
}<br>
<br>
/* Did we still not find it?*/<br>
if(!blFound)<br>
{<br>
/* Yup, this must be an error - abort.*/<br>
blDone = 1;<br>
blError = 1;<br>
<br>
/* Terminate the string.*/<br>
*pszCurrOut = ' ';<br>
}<br>
<br>
}<br>
<br>
/*Is it a space?*/<br>
else if(isspace(*pszCurrIn));<br>
{<br>
/* Well if it is, there must be one more and then a non-space after it..*/<br>
if(isspace(*(pszCurrIn + 1)) && !isspace(*(pszCurrIn + 2)))<br>
{<br>
/* Ok, it's a space (3 spaces in morse code representation.*/<br>
sprintf(pszCurrOut, &quot; &quot;);<br>
<br>
/* Increase outbuffer used count.*/<br>
i++;<br>
<br>
/* Jump ahead in the input buffer pas the spaces.*/<br>
pszCurrIn += 2;<br>
<br>
/* Go to the next lcoation in the buffer output.*/<br>
pszCurrOut++;<br>
}<br>
else<br>
{<br>
/* Well, next one isn't a space, or the one after that is, so this is*/<br>
/* a problem. Abort.*/<br>
blDone = 1;<br>
blError = 1;<br>
<br>
/* Terminate the string.*/<br>
*pszCurrOut = '\0';<br>
<br>
}<br>
}<br>
<br>
/* Is it end of string? */<br>
if('\0' == *pszCurrIn)<br>
{<br>
blDone = 1;<br>
*pszCurrOut = '\0';<br>
}<br>
<br>
/* Well, if we get down to here, it must be a problem.*/<br>
else<br>
{<br>
blDone = 1;<br>
blError = 0;<br>
<br>
pszCurrOut = '\0';<br>
}<br>
<br>
<br>
/* Did we get a done flag?*/<br>
if blDone <br>
{<br>
/* Nope, we ran out of buffer space. Terminate and signify an error.*/<br>
*pszCurrOut = '\0';<br>
blError = 1;<br>
}<br>
else<br>
<br>
/* Yup, terminate the output buffer.*/<br>
*pszCurrOut = '\0';<br>
<br>
<br>
/* Return 1 if successful, 0 if error.*/<br>
return !blError;<br>
<br>
}<br>
<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top