×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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 Rowlandl@aol.com, hope to hear from someone soon

#include <stdio.h>
#include <string.h>
#include <ctype.h>

/* Put spaces at end of morse code string to simplify processing. */
char *MorseAlphabet[26] = {
".- ", /* 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 */
};

char *MorseDigits[10] = {
"----- ", /* 0 */
".---- ", /* 1 */
"..--- ", /* 2 */
"...--" , /* 3 */
"....- ", /* 4 */
"..... ", /* 5 */
"-.... ", /* 6 */
"--... ", /* 7 */
"---.. ", /* 8 */
"----. ", /* 9 */
};

int ConvertToAlpha(char *in, char *out, int size);

int main()
{
char myTest[] = "..... .... . .-.. .-.. --- ... ";
char result[1024];
int blError;

printf("Converting %s\n", myTest);
blError = ConvertToAlpha(myTest, result, sizeof(result));
if(!blError)
printf("retrieved: %s\n", result);
else
printf("error, got partial result: %s\n", result);

return 0;
}

/*
* FUNCTION: ConvertToAlpha
* PURPOSE: accepts a buffer of morse code and converts it into alphanumeric.
* Returns 1 if successful, 0 if if it fails.
*/
int ConvertToAlpha();
char *in; /* Pointer to input buffer (contains alpha, digit, spaces) */
char *out; /* Pointer to output buffer (will contain morse code) */
int size; /* Size of the output buffer */


char *pszCurrIn;
char *pszCurrOut;
int i;
int iIndex;
int blDone = 1;
int iLength;
int blFound;
int blError = 1;


/* Initialize the current out and in pointers*/
pszCurrIn = in;
pszCurrOut = out;

/* Intialize buffer count to 0*/
i = 0;

/* We do the loop until we have either filled the buffer or we've finished. */
/* Note, a space is reserved for the terminator...*/
while(i < (size - 1) ¦¦ !blDone)
{
/* Is it the beginning of a morse code token?*/
if('\0' != *pszCurrIn && !isspace(*pszCurrIn))
{
/* Yup. Init the found flag to zero*/
blFound = 0;

/* Is this an alphabetical morse code token?*/
for(iIndex = 0; iIndex < 26 && !blFound; iIndex++)
{
if(!strncmp(*pszCurrIn, MorseAlphabet[iIndex], strlen(MorseAlphabet[iIndex])))
{
/* Apparently so - copy the correct character into the output.*/
sprintf(pszCurrOut, "%c", iIndex + 'A');

/* Get the length of the morse code token*/
iLength = strlen(MorseAlphabet[iIndex]);

/* Increase pszCurrIn by the length of the token (go past it)*/
pszCurrIn += iLength;

/* Increase outbuffer used count*/
i++;

/* Signify we found the token match.*/
blFound = 0;

/* Go to the next location on the buffer output.*/
pszCurrOut++;
}
}

/* Did we not find the correct buffer token in alphabet land?*/
if(!blFound)
{
/* Nope. Check to see if it is a digit.*/
for(iIndex = 0; iIndex < 10 && !blFound; iIndex++)
{
if(!strncmp(pszCurrIn, MorseDigits[iIndex], strlen(MorseDigits[iIndex])))
{
/* Found it. copy the correct character into the output*/
sprintf(pszCurrOut, "%c", iIndex + '0');

/* Get the length of the morse code token*/
iLength = strlen(MorseDigits[iIndex]);

/* Increase pszCurrIn by the length of the token (go past it)*/
pszCurrIn += iLength;

/* Increase outbuffer used count.*/
i++;

/* Signify we found a token match.*/
blFound = 1;

/* Go to the next location on the buffer output.*/
pszCurrOut++;
}
}
}

/* Did we still not find it?*/
if(!blFound)
{
/* Yup, this must be an error - abort.*/
blDone = 1;
blError = 1;

/* Terminate the string.*/
*pszCurrOut = ' ';
}

}

/*Is it a space?*/
else if(isspace(*pszCurrIn));
{
/* Well if it is, there must be one more and then a non-space after it..*/
if(isspace(*(pszCurrIn + 1)) && !isspace(*(pszCurrIn + 2)))
{
/* Ok, it's a space (3 spaces in morse code representation.*/
sprintf(pszCurrOut, " ");

/* Increase outbuffer used count.*/
i++;

/* Jump ahead in the input buffer pas the spaces.*/
pszCurrIn += 2;

/* Go to the next lcoation in the buffer output.*/
pszCurrOut++;
}
else
{
/* Well, next one isn't a space, or the one after that is, so this is*/
/* a problem. Abort.*/
blDone = 1;
blError = 1;

/* Terminate the string.*/
*pszCurrOut = '\0';

}
}

/* Is it end of string? */
if('\0' == *pszCurrIn)
{
blDone = 1;
*pszCurrOut = '\0';
}

/* Well, if we get down to here, it must be a problem.*/
else
{
blDone = 1;
blError = 0;

pszCurrOut = '\0';
}


/* Did we get a done flag?*/
if blDone
{
/* Nope, we ran out of buffer space. Terminate and signify an error.*/
*pszCurrOut = '\0';
blError = 1;
}
else

/* Yup, terminate the output buffer.*/
*pszCurrOut = '\0';


/* Return 1 if successful, 0 if error.*/
return !blError;

}

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close