Thread: Replacing Morse to English

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    11

    Post Replacing Morse to English

    Hi, working on a college project (only very new to C) and i'm trying to write a code that i input a bunch of morse into the program, but when it runs the morse is translated into english, i know i assume i have to have every morse equal to a letter or number with a space after, two spaces for a word break, then the program will check the sentence and replace each it reads with the correct given translation. I have no idea how to do this properly. The best i've done is a weird concoction that only reads the first character e.g the - or the . Any ideas?
    Thanks.
    -Teepee

  2. #2
    Registered User
    Join Date
    Sep 2019
    Posts
    11
    To add, sorry if i didn't use correct tags or post icons, just signed up and first post.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    It's hard to give you more ideas, if you don't show your current idea.

    Plus, it's hard to show you how without just giving you the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2019
    Posts
    11
    This is what i did originally to try, but i don't think that breaks and cases are the right way to go with it, maybe strings or arrays?



    Code:
    #include <stdio.h>
        
    
    
    main (){
    
    
    
    
    char enterMorse = '--'; 
    
    
    switch(enterMorse) {
        case '.-':
            printf("A");
        break;
        case '-...':
            printf("B");
        break;
        case '-.-.':
            printf("C");
        break;
        case '-..':
            printf("D");
        break;
        case '.':
            printf("E");
        break;
        case '..-.':
            printf("F");
        break;
        case '--.':
            printf("G");
        break;
        case '....':
            printf("H");
        break;
        case '..':
            printf("I");
        break;
        case '.---':
            printf("J");
        break;
        case '-.-':
            printf("K");
        break;
        case '.-..':
            printf("L");
        break;
        case '--':
            printf("M");
        break;
        case '-.':
            printf("N");
        break;
        case '---':
            printf("O");
        break;
        case '.--.':
            printf("P");
        break;
        case '--.-':
            printf("Q");
        break;
        case '.-.':
            printf("R");
        break;
        case '...':
            printf("S");
        break;
        case '-':
            printf("T");
        break;
        case ',,-':
            printf("U");
        break;
        case '...-':
            printf("V");
        break;
        case '.--':
            printf("W");
        break;
        case '-..-':
            printf("X");
        break;
        case '-.--':
            printf("Y");
        break;
        case '--..':
            printf("Z");
        break;
        case '-----':
            printf("0");
        break;
        case '.----':
            printf("1");
        break;
        case '..---':
            printf("2");
        break;
        case '...--':
            printf("3");
        break;
        case '....-':
            printf("4");
        break;
        case ',,,,,':
            printf("5");
        break;
        case '-....':
            printf ("6");
        break;
        case '--...':
            printf ("7");
        break;
        case '---..':
            printf ("8");
        break;
        case '----.':
            printf ("9");
        break;
        default:
            printf ("invaliddddddd");
        break;
    }
     printf ("%c\n", enterMorse);
    
    
    
    
        
        system("pause");
    it was just reading the first - in char entermorse

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    OK, a few things to sort out.

    1. 'c' != "c"
    In single quotes, you have a character constant, and it can only be a single character.
    In double quotes, it's a string, and you can have as many characters as you like.
    Whilst you can do switch('c'), you can't do switch("c"), so you have to resort to using strcmp.

    2. Split the problem into functions.
    Code:
    void morse ( const char *morse ) {
        // Now you can focus on just one letter.
    }
    
    int main ( ) {
      char morse[] = ".-";
      decodeMorse(morse);
    }
    3. Do you know about arrays? Do you know about structures?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,267
    I'm not familiar with multi-character literals, but as you discovered they don't work for you in this instance.

    Rather, input the morse code as a string which you can then break up into substrings, each corresponding to a single morse code "letter". Then you have a few possible approaches, e.g., computing a unique number from the morse code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2019
    Posts
    11
    Quote Originally Posted by Salem View Post

    3. Do you know about arrays? Do you know about structures?
    I kind of know arrays, not about structures, currently doing strings and arrays in lectures and classes. Not clicking yet though

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    You save a lot of copy/paste if you can do this.
    Code:
    char morseTable[][6] = {
        ".-",
        // etc
    };
    char letterTable[][2] = {
        "A",
        // etc
    };
    A for loop through one of them will give you an index into the other one - when a match is found.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Sep 2019
    Posts
    11
    Would the way you just showed be like this? and is the [6] and [2] how many i can have?
    Code:
     char morseTable[][26] = {
    Code:
        ".-", "..", "--",
        // etc
    };
    char letterTable[][26] = {
        "A","B", "c", "d",
        // etc
    }; 
    Last edited by Temphost; 09-16-2019 at 05:43 AM. Reason: cleaner

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    > Would the way you just showed be like this? and is the [6] and [2] how many i can have?
    That's the length of each string.

    char morseTable[numberOfMorseSymbols][maxLengthOfMorseCode]

    But the compiler will count the left-most dimension for you, based on how many initialisers you provide.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    148

    Post

    only reads the first character
    The problem:

    'a' is for characters.
    "abc" is for string.

    Double quotes is for string and single quotes for character.
    therefore: '--.' is invalid
    Last edited by Structure; 09-16-2019 at 06:58 AM.


  12. #12
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    148

    Post

    any ideas?
    Comparing strings can be difficult...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define morse_A ".-"
    #define morse_B "-..."
    #define morse_C "-.-."
    #define morse_D "-.."
    #define morse_E "."
    #define morse_F "..-."
    #define morse_G "--."
    #define morse_H "...."
    #define morse_I ".."
    #define morse_J ".---"
    #define morse_K "-.-"
    #define morse_L ".-.."
    #define morse_M "--"
    #define morse_N "-."
    #define morse_O "---"
    #define morse_P ".--."
    #define morse_Q "--.-"
    #define morse_R ".-."
    #define morse_S "..."
    #define morse_T "-"
    #define morse_U "..-"
    #define morse_V "...-"
    #define morse_W ".--"
    #define morse_X "-..-"
    #define morse_Y "-.--"
    #define morse_Z "--.."
    
    char morseEntered[10];
    int compare(char *string1,char *string2) {
      int response = 1;
      for (int i=0;i<=strlen(string1);i++) {
        if ( string1[i] != string2[i] ) response = 0;
      };
      return response;
    };
    
    int main(int argc, char *argv[]) {
      sprintf(morseEntered,".-");
      if ( compare(morseEntered,morse_A) ) printf("A");
      if ( compare(morseEntered,morse_B) ) printf("B");
      if ( compare(morseEntered,morse_C) ) printf("C");
      if ( compare(morseEntered,morse_D) ) printf("D");
      if ( compare(morseEntered,morse_E) ) printf("E");
      if ( compare(morseEntered,morse_F) ) printf("F");
      if ( compare(morseEntered,morse_G) ) printf("G");
      if ( compare(morseEntered,morse_H) ) printf("H");
      if ( compare(morseEntered,morse_I) ) printf("I");
      if ( compare(morseEntered,morse_J) ) printf("J");
      if ( compare(morseEntered,morse_K) ) printf("K");
      if ( compare(morseEntered,morse_L) ) printf("L");
      if ( compare(morseEntered,morse_M) ) printf("M");
      if ( compare(morseEntered,morse_N) ) printf("N");
      if ( compare(morseEntered,morse_O) ) printf("O");
      if ( compare(morseEntered,morse_P) ) printf("P");
      if ( compare(morseEntered,morse_Q) ) printf("Q");
      if ( compare(morseEntered,morse_R) ) printf("R");
      if ( compare(morseEntered,morse_S) ) printf("S");
      if ( compare(morseEntered,morse_T) ) printf("T");
      if ( compare(morseEntered,morse_U) ) printf("U");
      if ( compare(morseEntered,morse_V) ) printf("V");
      if ( compare(morseEntered,morse_W) ) printf("W");
      if ( compare(morseEntered,morse_X) ) printf("X");
      if ( compare(morseEntered,morse_Y) ) printf("Y");
      if ( compare(morseEntered,morse_Z) ) printf("Z");
      return 0;
    };
    Last edited by Structure; 09-16-2019 at 12:48 PM.


  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,267
    Quote Originally Posted by Structure View Post
    Comparing strings can be difficult...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define morse_A ".-"
    #define morse_B "-..."
    #define morse_C "-.-."
    #define morse_D "-.."
    #define morse_E "."
    #define morse_F "..-."
    #define morse_G "--."
    #define morse_H "...."
    #define morse_I ".."
    #define morse_J ".---"
    #define morse_K "-.-"
    #define morse_L ".-.."
    #define morse_M "--"
    #define morse_N "-."
    #define morse_O "---"
    #define morse_P ".--."
    #define morse_Q "--.-"
    #define morse_R ".-."
    #define morse_S "..."
    #define morse_T "-"
    #define morse_U "..-"
    #define morse_V "...-"
    #define morse_W ".--"
    #define morse_X "-..-"
    #define morse_Y "-.--"
    #define morse_Z "--.."
    
    char morseEntered[10];
    int compare(char *string1,char *string2) {
      int response = 1;
      for (int i=0;i<=strlen(string1);i++) {
        if ( string1[i] != string2[i] ) response = 0;
      };
      return response;
    };
    
    int main(int argc, char *argv[]) {
      sprintf(morseEntered,".-");
      if ( compare(morseEntered,morse_A) ) printf("A");
      if ( compare(morseEntered,morse_B) ) printf("B");
      if ( compare(morseEntered,morse_C) ) printf("C");
      if ( compare(morseEntered,morse_D) ) printf("D");
      if ( compare(morseEntered,morse_E) ) printf("E");
      if ( compare(morseEntered,morse_F) ) printf("F");
      if ( compare(morseEntered,morse_G) ) printf("G");
      if ( compare(morseEntered,morse_H) ) printf("H");
      if ( compare(morseEntered,morse_I) ) printf("I");
      if ( compare(morseEntered,morse_J) ) printf("J");
      if ( compare(morseEntered,morse_K) ) printf("K");
      if ( compare(morseEntered,morse_L) ) printf("L");
      if ( compare(morseEntered,morse_M) ) printf("M");
      if ( compare(morseEntered,morse_N) ) printf("N");
      if ( compare(morseEntered,morse_O) ) printf("O");
      if ( compare(morseEntered,morse_P) ) printf("P");
      if ( compare(morseEntered,morse_Q) ) printf("Q");
      if ( compare(morseEntered,morse_R) ) printf("R");
      if ( compare(morseEntered,morse_S) ) printf("S");
      if ( compare(morseEntered,morse_T) ) printf("T");
      if ( compare(morseEntered,morse_U) ) printf("U");
      if ( compare(morseEntered,morse_V) ) printf("V");
      if ( compare(morseEntered,morse_W) ) printf("W");
      if ( compare(morseEntered,morse_X) ) printf("X");
      if ( compare(morseEntered,morse_Y) ) printf("Y");
      if ( compare(morseEntered,morse_Z) ) printf("Z");
      return 0;
    };
    Assuming the compiler manages to identify that the strlen call in the loop condition is effectively a loop constant, this code has a similiar time complexity to what Salem suggested in post #8, except that it is inferior because of all that need to repeat the if statements instead of just using a loop.

    Note that you should use the standard strcmp (and family) instead of writing your own string comparison function(s) unless you have special reasons to do so.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    148
    you should use the standard strcmp (and family) instead of writing your own string comparison function(s) unless you have special reasons to do so.
    because i like to party.


  15. #15
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,516
    Quote Originally Posted by Structure View Post
    Comparing strings can be difficult...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define morse_A ".-"
    #define morse_B "-..."
    #define morse_C "-.-."
    #define morse_D "-.."
    #define morse_E "."
    #define morse_F "..-."
    #define morse_G "--."
    #define morse_H "...."
    #define morse_I ".."
    #define morse_J ".---"
    #define morse_K "-.-"
    #define morse_L ".-.."
    #define morse_M "--"
    #define morse_N "-."
    #define morse_O "---"
    #define morse_P ".--."
    #define morse_Q "--.-"
    #define morse_R ".-."
    #define morse_S "..."
    #define morse_T "-"
    #define morse_U "..-"
    #define morse_V "...-"
    #define morse_W ".--"
    #define morse_X "-..-"
    #define morse_Y "-.--"
    #define morse_Z "--.."
    
    char morseEntered[10];
    int compare(char *string1,char *string2) {
      int response = 1;
      for (int i=0;i<=strlen(string1);i++) {
        if ( string1[i] != string2[i] ) response = 0;
      };
      return response;
    };
    
    int main(int argc, char *argv[]) {
      sprintf(morseEntered,".-");
      if ( compare(morseEntered,morse_A) ) printf("A");
      if ( compare(morseEntered,morse_B) ) printf("B");
      if ( compare(morseEntered,morse_C) ) printf("C");
      if ( compare(morseEntered,morse_D) ) printf("D");
      if ( compare(morseEntered,morse_E) ) printf("E");
      if ( compare(morseEntered,morse_F) ) printf("F");
      if ( compare(morseEntered,morse_G) ) printf("G");
      if ( compare(morseEntered,morse_H) ) printf("H");
      if ( compare(morseEntered,morse_I) ) printf("I");
      if ( compare(morseEntered,morse_J) ) printf("J");
      if ( compare(morseEntered,morse_K) ) printf("K");
      if ( compare(morseEntered,morse_L) ) printf("L");
      if ( compare(morseEntered,morse_M) ) printf("M");
      if ( compare(morseEntered,morse_N) ) printf("N");
      if ( compare(morseEntered,morse_O) ) printf("O");
      if ( compare(morseEntered,morse_P) ) printf("P");
      if ( compare(morseEntered,morse_Q) ) printf("Q");
      if ( compare(morseEntered,morse_R) ) printf("R");
      if ( compare(morseEntered,morse_S) ) printf("S");
      if ( compare(morseEntered,morse_T) ) printf("T");
      if ( compare(morseEntered,morse_U) ) printf("U");
      if ( compare(morseEntered,morse_V) ) printf("V");
      if ( compare(morseEntered,morse_W) ) printf("W");
      if ( compare(morseEntered,morse_X) ) printf("X");
      if ( compare(morseEntered,morse_Y) ) printf("Y");
      if ( compare(morseEntered,morse_Z) ) printf("Z");
      return 0;
    };
    Your compare function still has undefined behaviour if the length of string1 is longer than string2's length (because would be reading/comparing beyond the end of string2). Try adding a break.

    For example:

    Code:
    int compare(char *string1,char *string2) {
      int response = 1;
      for (int i=0;i<=strlen(string1);i++) {
        if ( string1[i] != string2[i] ) {
             response = 0;
             break;
        }
      };
      return response;
    };
    At least it will work now even though you're relying on the compiler to make the assumption that the strlen() call is invariant for the loop. Assuming it does it's not really the most obvious way to write a function that compares strings.
    The is at least one other problem: string1 and string2 not const qualified (i.e. the function prototype should really be int compare(const char *string1, const char *string2)). Another potential problem is the return value which is, for example, different to what the standard function strcmp() would return. If you really want or need the return value to be either 0 or 1 and nothing else why not write !!strcmp(morseEntered,morse_A). I can't see the point of this compare() function
    Last edited by Hodor; 09-17-2019 at 01:13 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-04-2018, 08:16 AM
  2. Code Morse ,help...
    By Raskolnikov LM in forum C Programming
    Replies: 1
    Last Post: 01-05-2018, 05:24 PM
  3. Morse Code(Going the other way)
    By lilbo4231 in forum C Programming
    Replies: 21
    Last Post: 06-16-2011, 03:25 AM
  4. Implementing a English-Spanish/Spanish-English Dictionary
    By invertedMirrors in forum C Programming
    Replies: 4
    Last Post: 02-23-2008, 03:48 PM
  5. how convert english to morse code
    By uler in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2001, 07:56 PM

Tags for this Thread