Thread: how convert english to morse code

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    4

    Question how convert english to morse code

    hi all, I'm new at this so be nice.
    Below is a code for converting a given statement in english into morse code. My problem is that it is not printing to screen beyond the first "word".
    Can anyone see where the problem might be?
    between letters should be one space, between words should be three spaces.

    sorry about the formatting but it went all goobly when I copied/pasted

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

    typedef struct{
    char morse[6];
    char letter;
    }morsecode;

    int main(){

    morsecode trans[36]={
    {".-", 65}, {"-...", 66}, {"-.-.", 67}, {"-..", 68}, {".", 69}, {"..-.", 70},
    {"--.", 71}, {"....", 72}, {"..", 73}, {".---", 74}, {"-.-", 75}, {".-..", 76},
    {"--", 77}, {"-.", 78}, {"---", 79}, {".--.", 80}, {"--.-", 81}, {".-.", 82},
    {"...", 83}, {"-", 84}, {"..-", 85}, {"...-", 86}, {".--", 87}, {"-..-", 88},
    {"-.--", 89}, {"--..", 90}, {"-----", 48}, {".----", 49}, {"..---", 50},
    {"...--", 51}, {"....-", 52}, {".....", 53}, {"-....", 54}, {"--...", 55},
    {"---..", 56}, {"----.", 57}
    };
    char phrase[34] = {"DUDE WHERES THE 1978 GOGGO MOBILE"};
    int search, index;
    for(index = 0; index <= 33; index++){
    for(search = 0; search <= 35; search++){
    if(trans[search].letter == phrase[index]){
    printf("%s ", trans[search].morse); }else if(phrase[index] == ' '){
    printf(" ");
    }else{
    printf("\nError Char not found!!!\n");
    }
    }
    }

    return 0;
    }

  2. #2
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Make certain to set proper break points or you will get unexpected results.
    Code:
    for(index = 0; index <= 33; index++)
    { 
        for(search = 0; search <= 35; search++)
        { 
            if(trans[search].letter == phrase[index])
            { 
                printf("%s ", trans[search].morse);
                break; // found it - get out of inner loop
            }
            else if(phrase[index] == ' ')
            { 
                printf(" "); 
                break; // found it - get out of inner loop
            }
            // this is unnecessary
            // else
            // { 
            //     printf("\nError Char not found!!!\n"); 
            // } 
        } 
    }
    Try this and see if it helps.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4

    Cool

    Lord Aragorn (Strider),

    your help is much appreciated!

    Uler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  2. C decoder for files encoded in Morse code
    By amsy in forum C Programming
    Replies: 5
    Last Post: 10-22-2004, 12:40 AM
  3. Code to convert temperature
    By Anna Lane in forum C Programming
    Replies: 2
    Last Post: 10-14-2002, 08:18 PM
  4. Morse Code Translator!
    By Paro in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2002, 07:23 PM