how do I morse code converting program

This is a discussion on how do I morse code converting program within the C Programming forums, part of the General Programming Boards category; I was wondering if anyone could help me with this... I'm trying to write a program that takes an input ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    84

    how do I morse code converting program

    I was wondering if anyone could help me with this... I'm trying to write a program that takes an input txt file, reads a line (either in english or in morse code ( ie --.- ) and converts it to its counterpart...

    I wrote a code that can scan the data one line at a time, I want to test the line to see if it consist of letters, or dashes and dots (morse code) so I can send it to an appropriate function to convert it....

    I can't figure out how to test for this though, I'm trying to make a conditional statement say

    Code:
    if( line == char ) 
    {}
    does anyone know a way I may be able to test for this...

    so far my code looks like this

    Code:
    main() {
           
         FILE *fin,*fout;
         fin = fopen("morse.txt","r");
         fout = fopen("morse_out.txt","w"); 
         
         char line[Max_Size + 1];  /* This is used to store the output lines */
         
         
              while ( fgets( line, Max_Size + 1, fin ) != NULL )     
         {              
               fprintf(fout,"%s", line);
               fprintf(fout,"WHERE", line);
              
         }
    }
    it says where cause I am trying to get it to stop after it reads a line so i can test it

    thank you, any help/suggestions are greatly aprreciated

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    To determine whether the code is morse or text, the easiest way would be to check if it's morse. This is a fairly simple procedure, since morse should only contain '-' '.' possibly '\n' and ' '.

    Read in the file to your string then loop through the string one character at a time to find anything that isn't one of those characters.

    Code:
    int x = 0;
    bool isMorse = true;
    do {
        if (string1[x] != '-' && string1[x] != '.' && string1[x] != ' ' && string1[x] != '\n' && string1[x] != '\0') {
            isMorse = false
        }
        x++
    } while (x <= strlen(string1) && isMorse == true);
    Last edited by SlyMaelstrom; 10-29-2005 at 02:33 AM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    line is a character array, you can't compare it to a single char.

    However you can easily test if the line consists only of letters, or only of dots and dashes. You can iterate through the array and test each character - if each character passes the test, the whole line is good. So for instance, you might look at the first character. If it's a letter, you know you are dealing with letters. If it's a '-' or a '.' you know you are dealing with morse code. Then go along the array and make sure each character is either a letter, or a '-' or a '.', depending on which the first character was. If not all characters match, your input is invalid. If you do it this way, the isalpha() function will be helpful.

    Alternately, and probably easier, you could use sscanf. You could use a line like this:
    Code:
    sscanf(line, "%[-. \t\n] ", line);
    to match morse code. I have included spaces, tabs and newlines as valid characters, omit them or add more as you require. To ensure that the whole string was matched, you can check strlen(line) before and after and make sure they are the same. If they are not, some characters did not match.

    To do a similar thing with letters, you could use:
    Code:
    sscanf(line, "%[A-Za-z \t\n] ", line);
    Again, if you want more or less valid characters, just change what's inside the []'s.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    OOooo I like your alternative method better than mine. I just wouldn't rescan into the same variable, because he wants to do stuff with it afterward.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    84

    thank you

    Thank you for your help, does it matter that my string which consist of letters may also include periods and hyphens in the text?

  6. #6
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    SlyMaelstrom: You could of course read into another variable, I just figured he wouldn't want to do anything with line if the input was invalid, and if it is valid, line will not change.

    panfilero: To match other characters as well as letters, add them to the brackets, for instance, if you wanted to include punctuation characters you might do: [A-Za-z,.-?! \t\n]. You could also add any other punctuation you consider to be valid input. If you are going to have a lot of punctuation though, it might be a good idea to #define your set of characters and make the format string at runtime.

    If you are going to include periods and hyphens in your plaintext input, I would suggest scanning for morse code first, as your check letters would also match morse code.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    No. In the code we're showing you, it's checking to see if it's all morse code. If at any point the string contains a value that isn't a proper morse code value, it returns false.

    So for example, let me give three possiblilites for line.
    Code:
     abd 42 ..d ews    //Example one when passed through the loop will immediately note that 'a' is not an acceptable morse code character
    //and it will say the string is text.
    ....--..----..-----..--a  // This is also text. It will go through the string without fail until it hits the a and classifies it as text
    -----...----.----..-----\n--..--- -.. // Finally this is morse code. Every character in the string meets the paramaters for morse code and the value of isMorse will never change.
    Sent from my iPadŽ

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Quote Originally Posted by lemnisca
    SlyMaelstrom: You could of course read into another variable, I just figured he wouldn't want to do anything with line if the input was invalid, and if it is valid, line will not change.
    Code:
    sscanf(line, "%[-. \t\n] ", line);
    This doesn't check to see if the input is valid, it checks to see if it's morse code or not. In the case that it isn't morse code then you'll have changed the text that he wants to convert to morse code. In either case, morse code or not, he doesn't want to change what's in the string. That's what I was trying to say.
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    I have this declared as a global variable:

    Code:
    char morsecode[45][8] = {
          "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--..","0-----",
          "1.----","2..---","3...--","4....-","5.....","6-....","7--...",
          "8---..","9----.","..-.-.-",",--..--",":---...","?..--..","'.----.",
          "--....-","/-..-.","(-.--.-","\".-..-."};

    So I was going to try and use this to check my string like this:

    Code:
        /***** Loop that checks is string is Morse Code or English ****/
               
               for ( x = 0 ; x < length ; x++ ) {
                   
                   for ( k=0; k <= 25 ;k++){
               
               if ( string1[x] == morsecode[k][0] ) {
                  is_Morse = 0; 
                  break;} 
                  else is_Morse = 1;
                  }
                  }
                  
        
                       
    if( is_Morse == 1 ) {
        fprintf (fout," Morse to English Goes here \n "); }
    if( is_Morse == 0 ){
        fprintf(fout," English to Morse Goes Here \n"); }
             
    }
    But for some reason after every string it prints "Morse to english goes here"

    Is my logic wrong here? I think this code is checking to see if any part of a string is a letter, and if it is to say is_Morse == 0

    I dont know why it isn't working.


    Thank You

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Whoa whoa whoa... you're making a mess with that global array. It's ugly and it's completely unnessasary. Look at how many values you have to check to see if it's in plain text.

    Using my code exactly
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main() {
        char string1[100];    
        int x = 0;
        int isMorse = 1;
        strcpy(string1, "-----..----.---.---...-");
        
        do {
           if (string1[x] != '-' && string1[x] != '.' && string1[x] != ' ' && 
               string1[x] != '\n' && string1[x] != '\0') {
              isMorse = 0;
           }
           x++;
        } while (x <= strlen(string1) && isMorse == 1);
    
        if( isMorse == 1 ) {
            printf (" Morse to English Goes here \n "); 
        }
        if( isMorse == 0 ){
            printf(" English to Morse Goes Here \n"); 
        }
        
        return 0;
    }
    This should do what you want. Check to see if it's morse or text. Also, that break; in your program is useless and always a bad keyword to use.

    Edit: Changed isMorse to an int, incase your compiler doesn't understand bool.
    Edit: Gave you the full program. Just change the value of the string as you wish to see how it works.
    Last edited by SlyMaelstrom; 10-29-2005 at 03:49 AM.
    Sent from my iPadŽ

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    Thanks, your codes is working, I wish I could understand it better, I'm not sure how it is distinguishing between morse and english without getting confused by the english that contains periods and hyphens

    appreciate the help

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    ...and this is what lemnisca was suggesting and I agree, it's a much better, much more compact method.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main() {
        char string1[100], string2[100];    
        strcpy(string1, "hello");
        
        sscanf(string1, "%[-. \t\n] ", string2);
    
        if(strcmp(string1, string2) == 0 ) {
            printf (" Morse to English Goes here \n "); 
        }
        else {
            printf(" English to Morse Goes Here \n"); 
        }
        getchar();
        
        return 0;
    }
    Quote Originally Posted by panfilero
    Thanks, your codes is working, I wish I could understand it better, I'm not sure how it is distinguishing between morse and english without getting confused by the english that contains periods and hyphens

    appreciate the help
    As long as your string contains a character that isn't a decimal or a dash, no matter how many millions of decimals it has, it will be considered text.

    .................................................. ....1......................
    This is text not morse
    Last edited by SlyMaelstrom; 10-29-2005 at 03:56 AM.
    Sent from my iPadŽ

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    84

    Morse Code

    Does this code look bad to you? I'm trying to turn the string1 array from english into morse code. But it says I'm trying to compare a pointer and an interger.... i think the logic is ok, I don't see why it's giving me this error.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define Max_Size 80
    
    char morsecode[45][8] = {
          "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--..","0-----",
          "1.----","2..---","3...--","4....-","5.....","6-....","7--...",
          "8---..","9----.","..-.-.-",",--..--",":---...","?..--..","'.----.",
          "--....-","/-..-.","(-.--.-","\".-..-."};
     
    char string1[] = { 'H','e','l','l','o' };                
                            
                 
    main() { 
      
      int length;     
      int x;
      int n = 1;
      int k;     
      char string2[80];
      
         length = strlen( string1);  
           
           
         for(x=0;x<=length;x++){
                                  
           for(k=0;k<=48;k++) {
                            
               if( string1[x] == morsecode[k][0] ) {
                   
                   while( morsecode[k][n] != NULL ) {
                          
                          string2[n-1] = morsecode[k][n];
                          
                          n += 1;  }
                          
                          }
                          
                          }

  14. #14
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    I don't understand why that doesnt work, because when i do this, I get no error.... (This is just the last while loop)

    Code:
     
    
     
     
     #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define Max_Size 80
    
    char morsecode[45][8] = {
          "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--..","0-----",
          "1.----","2..---","3...--","4....-","5.....","6-....","7--...",
          "8---..","9----.","..-.-.-",",--..--",":---...","?..--..","'.----.",
          "--....-","/-..-.","(-.--.-","\".-..-."};
     
    char string1[] = { 'H','e','l','l','o' };                
                 
                 
                 
    main() { 
      
      int length;     
      int x=3;
      int n = 1;
      int k;     
      char string2[80];
      
         length = strlen( string1);   
          
            
           
           while( morsecode[x][n] != NULL ) {
           
           string2[n-1] = morsecode[3][n];
           
           n += 1;
           }
           
           printf("%s", string2);
             
           
           getchar();}

  15. #15
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    What your nesting, you were missing two braces. Other than that, I compiled fine. Your logic is all wrong though, so you're not converting anything into morse code.

    Here, this compiled for me:
    Code:
    #include <stdio.h>
    #include <string.h>
    #define Max_Size 80
    
    char morsecode[45][8] = {
          "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--..","0-----",
          "1.----","2..---","3...--","4....-","5.....","6-....","7--...",
          "8---..","9----.","..-.-.-",",--..--",":---...","?..--..","'.----.",
          "--....-","/-..-.","(-.--.-","\".-..-."};
     
    char string1[] = { 'H','e','l','l','o' };                
                                        
    main() { 
         
      int x;
      int n = 1;
      int k;     
      char string2[80]; 
           
           
         for(x=0;x<=strlen(string1);x++){                       
           for(k=0;k<=48;k++) {            
             if( string1[x] == morsecode[k][0] ) {
                while( morsecode[k][n] != NULL ) {
                          
                          string2[n-1] = morsecode[k][n];
                          
                          n += 1;  }
                          
                }
             }
           } /* x <= length loop */
           printf("%s", string1);
    
           return 0;
    }
    Sent from my iPadŽ

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. How to make a program that prints it's own source code???
    By chottachatri in forum C++ Programming
    Replies: 38
    Last Post: 03-28-2008, 07:06 PM
  3. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 05:16 AM
  4. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  5. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21