We have been assigned to do a project from Deitel/Deitel's "C How to Program" 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 <stdio.h><br>
#include <string.h><br>
#include <ctype.h><br>
<br>
/* Put spaces at end of morse code string to simplify processing. */<br>
char *MorseAlphabet[26] = {<br>
".- ", /* A */<br>
"-... ", /* B */<br>
"-.-. ", /* C */<br>
"-.. ", /* D */<br>
". ", /* E */<br>
"..-. ", /* F */<br>
"--. " , /* G */<br>
".... ", /* H */<br>
".. " , /* I */<br>
".--- ", /* J */<br>
"-.- ", /* K */<br>
".-.. ", /* L */<br>
"-- ", /* M */<br>
"-. ", /* N */<br>
"--- ", /* O */<br>
".--.", /* P */<br>
"--.- ", /* Q */<br>
".-. ", /* R */<br>
"... ", /* S */<br>
"- ", /* T */<br>
"..- ", /* U */<br>
"...- ", /* V */<br>
".-- ", /* W */<br>
"-..- ", /* X */<br>
"-.--", /* Y */<br>
"--.. ", /* Z */<br>
};<br>
<br>
char *MorseDigits[10] = {<br>
"----- ", /* 0 */<br>
".---- ", /* 1 */<br>
"..--- ", /* 2 */<br>
"...--" , /* 3 */<br>
"....- ", /* 4 */<br>
"..... ", /* 5 */<br>
"-.... ", /* 6 */<br>
"--... ", /* 7 */<br>
"---.. ", /* 8 */<br>
"----. ", /* 9 */<br>
};<br>
<br>
int ConvertToAlpha(char *in, char *out, int size);<br>
<br>
int main()<br>
{<br>
char myTest[] = "..... .... . .-.. .-.. --- ... ";<br>
char result[1024];<br>
int blError;<br>
<br>
printf("Converting %s\n", myTest);<br>
blError = ConvertToAlpha(myTest, result, sizeof(result));<br>
if(!blError)<br>
printf("retrieved: %s\n", result);<br>
else<br>
printf("error, got partial result: %s\n", 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 < (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 < 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, "%c", 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 < 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, "%c", 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, " "<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>
<br>
#include <stdio.h><br>
#include <string.h><br>
#include <ctype.h><br>
<br>
/* Put spaces at end of morse code string to simplify processing. */<br>
char *MorseAlphabet[26] = {<br>
".- ", /* A */<br>
"-... ", /* B */<br>
"-.-. ", /* C */<br>
"-.. ", /* D */<br>
". ", /* E */<br>
"..-. ", /* F */<br>
"--. " , /* G */<br>
".... ", /* H */<br>
".. " , /* I */<br>
".--- ", /* J */<br>
"-.- ", /* K */<br>
".-.. ", /* L */<br>
"-- ", /* M */<br>
"-. ", /* N */<br>
"--- ", /* O */<br>
".--.", /* P */<br>
"--.- ", /* Q */<br>
".-. ", /* R */<br>
"... ", /* S */<br>
"- ", /* T */<br>
"..- ", /* U */<br>
"...- ", /* V */<br>
".-- ", /* W */<br>
"-..- ", /* X */<br>
"-.--", /* Y */<br>
"--.. ", /* Z */<br>
};<br>
<br>
char *MorseDigits[10] = {<br>
"----- ", /* 0 */<br>
".---- ", /* 1 */<br>
"..--- ", /* 2 */<br>
"...--" , /* 3 */<br>
"....- ", /* 4 */<br>
"..... ", /* 5 */<br>
"-.... ", /* 6 */<br>
"--... ", /* 7 */<br>
"---.. ", /* 8 */<br>
"----. ", /* 9 */<br>
};<br>
<br>
int ConvertToAlpha(char *in, char *out, int size);<br>
<br>
int main()<br>
{<br>
char myTest[] = "..... .... . .-.. .-.. --- ... ";<br>
char result[1024];<br>
int blError;<br>
<br>
printf("Converting %s\n", myTest);<br>
blError = ConvertToAlpha(myTest, result, sizeof(result));<br>
if(!blError)<br>
printf("retrieved: %s\n", result);<br>
else<br>
printf("error, got partial result: %s\n", 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 < (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 < 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, "%c", 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 < 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, "%c", 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, " "<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>