Ibrahim M. Eshera
Ibrahim M. Eshera

Reputation: 25

What is a better way to do this? Morse Code Program

I wrote a program in C for my homework that is supposed to take in text and translate it to Morse Code via an LED. For know I have substituted the pre-determined lengths of a blink from an LED with: "It is an __." This works as it is now and I would get full credit but my question is whether there is a better way to do this? I know there must be, instead of using all those 'if' statements. But I am a beginner in C. (Only drawback of this code is it is long and you cannot enter spaces.) The way it currently works is that it takes in a string and breaks it into individual letters, then in the 'for' loop checks these individual letters for the corresponding Morse Code. Let me know what you think.

// MorseCode_Attempt1.cpp : Defines the entry point for the console application.
//

include<stdio.h>
include<conio.h>
include<string.h>


void main() {

    char str[500];

    printf("Enter a string you want in Morse Code with underscores as spaces: ");
    scanf("%s", &str);

    int i;
    int stringLength = strlen(str);

    for (i = 0; i < stringLength; i++) {
        printf("\n[%c]\n", str[i]);


        if (str[i] == 'a') {
            printf("\nIt is an a.\n");
        }

        if (str[i] == 'b') {
            printf("\nIt is an b.\n");
        }

        if (str[i] == 'c') {
            printf("\nIt is an e.\n");
        }

        if (str[i] == 'd') {
            printf("\nIt is an d.\n");
        }

        if (str[i] == 'e') {
            printf("\nIt is an e.\n");
        }

        if (str[i] == 'f') {
            printf("\nIt is an f.\n");
        }

        if (str[i] == 'g') {
            printf("\nIt is an g.\n");
        }

        if (str[i] == 'h') {
            printf("\nIt is an h.\n");
        }

        if (str[i] == 'i') {
            printf("\nIt is an i.\n");
        }

        if (str[i] == 'j') {
            printf("\nIt is an j.\n");
        }

        if (str[i] == 'k') {
            printf("\nIt is an k.\n");
        }

        if (str[i] == 'l') {
            printf("\nIt is an l.\n");
        }

        if (str[i] == 'm') {
            printf("\nIt is an m.\n");
        }

        if (str[i] == 'n') {
            printf("\nIt is an n.\n");
        }

        if (str[i] == 'o') {
            printf("\nIt is an o.\n");
        }

        if (str[i] == 'p') {
            printf("\nIt is an p.\n");
        }

        if (str[i] == 'q') {
            printf("\nIt is an q.\n");
        }

        if (str[i] == 'r') {
            printf("\nIt is an r.\n");
        }

        if (str[i] == 's') {
            printf("\nIt is an s.\n");
        }

        if (str[i] == 't') {
            printf("\nIt is an t.\n");
        }

        if (str[i] == 'u') {
            printf("\nIt is an u.\n");
        }

        if (str[i] == 'v') {
            printf("\nIt is an v.\n");
        }

        if (str[i] == 'w') {
            printf("\nIt is an w.\n");
        }

        if (str[i] == 'x') {
            printf("\nIt is an x.\n");
        }

        if (str[i] == 'y') {
            printf("\nIt is an y.\n");
        }

        if (str[i] == 'z') {
            printf("\nIt is an z.\n");
        }

        if (str[i] == '_') {
            printf("\nIt is a SPACE.\n");
        }

    }

    return 0;

}

Upvotes: 1

Views: 2748

Answers (3)

user3629249
user3629249

Reputation: 16540

given your question, the following code should lead you in the right direction.

Notice that function: out(), function: delay(), #define ON and #define OFF depend on your environment.

include<stdio.h>
//include<conio.h> -- dont include header files that are not used
include<string.h>

// adjust DELAY_UNIT as necessary
#define DELAY_UNIT (1000)
#define DOT_TIME   (1)
#define DASH_TIME  (3)
#define INTER_WORD_TIME (7)
#define INTER_CHAR_TIME (3)
#define DOT        ('.')
#define DASH       ('-')

// OP needs to define function 'out()', LED, ON, OFF and DELAY_UNIT
// to suit the environment

// prototypes
char *findIndex( struct morseStruct *morse, char ch);
void MorseToLed( char *sequencePtr);
void delay( long unsigned int );

/*
 * If the duration of a dot is taken to be one unit
 * then that of a dash is three units.
 *
 * The space between the components of one character is one unit,
 *
 * between characters is three units and between words seven units.
 *
 * To indicate that a mistake has been made and for the receiver to delete the last word,
 * send ........ (eight dots).
 */

// see <http://morsecode.scphillips.com/morse2.html> for source list
// of morse code char formats

struct morseStruct
{
    char  searchChar;
    char *morseSequence;
};

static const struct morseStruct *alphanumeric[] =
{
    {'A', ",-"   };
    {'B', "-..." };
    {'C', "-.-." };
    ...  // add other alphameric definitions here
    {0, NULL};
};

static const struct morseStruct *punctuation[] =
{
    {'.', ".-.-.-" }; // period
    {',', "--..--" }; // comma
    {':', "---..." }; // colon
    {'?', "..--.." }; // question mark
    {'\'', ".----."}; // apostrophe (note: must escape certain characters)
    {'-', "-....-" }; // hyphen
    {'/', "-..-."  }; // right slash
    {'\"', ".-..-."}; // quotation mark
    {'@', ".--.-." }; // at sign
    {'=', "-...-"  }; // equals sign
    {0, NULL};
};

static const struct morseStruct *prosigns[] =
{
    ... // add prosigns here
    {0, NULL};
};



int main( void )
{

    char str[500];

    printf("Enter a string you want in Morse Code with underscores as spaces: ");
    if( NULL == fgets(str, sizeof( str ), stdin );
    { // then fgets failed
        perror( "fgets for user input string failed");
        exit( EXIT_FAILURE );
    }

    // implied else, fgets successful

    //eliminate trailing newline, if any
    if( char *pNewline = strstr( str, "\n")
    {
        pNewline = '\n';
    }

    size_t stringLength = strlen(str);
    int i;

    for (i = 0; i < stringLength; i++)
    {
        printf("\n[%c]\n", str[i]);


        // handle special cases
        if '_' == str[i] )
        { // then between words
            //out(LED, OFF); // should already be OFF
            delay( INTER_WORD_TIME*DELAY_UNIT );
            continue;
        }

        int sequencePtr;

        if( NULL == (sequencePtr = findIndex( alphaNumeric, str[i] ) ) )
        {// then not alphanumeric value
            if( NULL == (sequencePtr = findIndex( punctuation, str[i] ) ) )
            { // then not punctuation value
                if( NULL == (sequencePtr = findIndex( prosigns, str[i] ) ) )
                { // then not prosign value
                    printf( "char: %0X is not recognized\n", str[i]);
                    continue;
                }
            }
        }

        MorseToLed (sequencePtr);
    } // end for

    return 0;
} // end function: main


char *findIndex( struct morseStruct *morse, char ch)
{
    char *pSequence = NULL;
    int  i = 0;

    while( NULL != morse[i].morseSequence )
    {
        if( ch == morse[i].searchChar )
        { // then found desire morse code sequence
            pSequence = morse[i].morseSequence;
            break;
        }
        i++;
    }
    return( pSequence );
} // end function: findIndex


void MorseToLed( char *sequencePtr)
{
    size_t i = strlen( sequencePtr );

    for( size_t j = 0; j<i; j++)
    {
        if( DOT == sequencePtr[j] )
        {
            out(LED, ON);
            delay( DOT_TIME * DELAY_UNIT );
            out(LED, OFF);
        }
        else if( DASH == sequencePtr[j] )
        {
            out(LED, ON);
            delay( DASH_TIME * DELAY_UNIT );
            out(LED, OFF);
        }
        delay( DELAY_UNIT ); // delay between components of one char
    } // end for

    delay( INTER_CHAR_TIME * DELAY_UNIT ); // delay between characters
} // end function: MorseToLed


void delay( long unsigned int delayUnits )
{
    ?????
} // end function: delay

Upvotes: 0

Ulli
Ulli

Reputation: 11

Try this:

    for (...)
    {
       char c = str[i];
       unsigned Num;

       /* If c is a letter map it to 0..25 */
       Num = (unsigned)(toupper(c)-'A');
       if (Num<26) //This matches only with letters, as Num is unsigned.
       {
          //Each letter is represented by max four symbols 
          static const char MorseCodes[26][5]= 
          {
             "._",   //Morsecode for 'a'
             "_...", //Morsecode for 'b'
          ...
             "__..", //Morsecode for 'z'
          };
          printf("You have entered the letter %c. The morse code is %s\n", Num+'A', MorseCodes[Num]);
       }
       else 
       if (c=='_')
       {
          printf ("Space\n");
       } 
       else 
       {
          printf ("What You Are Doing?\n");
       } 
    }

Upvotes: 1

Riddhesh Sanghvi
Riddhesh Sanghvi

Reputation: 1222

When you are using a lot of if's and after one match you have to move on then use if-else-if ladder so that if say 'b' is found then it won't then check all the other conditions.

But the best solution here is a switch case.

Try something like this in yourfor-loop.

switch (str[i])
{
 case 'a':
   printf("\nIt is an a.\n");
   break;

 case 'b':
   printf("\nIt is a b.\n");
   break;

  /* etc. etc. etc.*/

 default:
 //default will work when the input to the switch->here str[i] does not match any case.
   printf("\nNot a character or space!");
   break;
}

Upvotes: 2

Related Questions