Search code examples
cmorse-code

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;

}

Solution

  • 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;
    }