Morse Code

Hi

Another assignment. I need to write the programme that implements the function that converts a string of English characters into and equivalent string of Morse code.

I don't know if this is a 'convension' assignment for the beginners to go thru but, I want to get some ideas of how to do.

Before asking the whole code(!), I want to know if the only way to achive this is to write something like the following?

- Assignment requirment is 'assume that only upper case letters are handled
- I have the Morse code list on my hand

[blue]
char English[] ={'A','B','C'.....'Z'};
char Morse[]= {'.-','-...','-.-.'.....'--..'};
[/blue]

So, for example, if I type;

[blue]
AB
[/blue]

Then, the output will be
[blue]
.--...
[/blue]


Thanks!

Comments

  • : Hi
    :
    : Another assignment. I need to write the programme that implements the function that converts a string of English characters into and equivalent string of Morse code.
    :
    : I don't know if this is a 'convension' assignment for the beginners to go thru but, I want to get some ideas of how to do.
    :
    : Before asking the whole code(!), I want to know if the only way to achive this is to write something like the following?
    :
    : - Assignment requirment is 'assume that only upper case letters are handled
    : - I have the Morse code list on my hand
    :
    : [blue]
    : char English[] ={'A','B','C'.....'Z'};
    : char Morse[]= {'.-','-...','-.-.'.....'--..'};
    : [/blue]
    :
    : So, for example, if I type;
    :
    : [blue]
    : AB
    : [/blue]
    :
    : Then, the output will be
    : [blue]
    : .--...
    : [/blue]
    :
    :
    : Thanks!
    :


    You don't have to make a table containing the letters, you could use the compiler's ASCII-table:

    char Morse[][6] = {".-","-...","-.-.",".....","--.."};
    char ch;

    scanf("%c", &ch);

    check that ch is a valid letter

    printf("%s
    ", Morse[ch-'A']);


    It might also be fun to make a function that goes through the Morse-string and make the PC's buzzer to beep long or short depending on symbol.
  • : : Hi
    : :
    : : Another assignment. I need to write the programme that implements the function that converts a string of English characters into and equivalent string of Morse code.
    : :
    : : I don't know if this is a 'convension' assignment for the beginners to go thru but, I want to get some ideas of how to do.
    : :
    : : Before asking the whole code(!), I want to know if the only way to achive this is to write something like the following?
    : :
    : : - Assignment requirment is 'assume that only upper case letters are handled
    : : - I have the Morse code list on my hand
    : :
    : : [blue]
    : : char English[] ={'A','B','C'.....'Z'};
    : : char Morse[]= {'.-','-...','-.-.'.....'--..'};
    : : [/blue]
    : :
    : : So, for example, if I type;
    : :
    : : [blue]
    : : AB
    : : [/blue]
    : :
    : : Then, the output will be
    : : [blue]
    : : .--...
    : : [/blue]
    : :
    : :
    : : Thanks!
    : :
    :
    :
    : You don't have to make a table containing the letters, you could use the compiler's ASCII-table:
    :
    : char Morse[][6] = {".-","-...","-.-.",".....","--.."};
    : char ch;
    :
    : scanf("%c", &ch);
    :
    : check that ch is a valid letter
    :
    : printf("%s
    ", Morse[ch-'A']);
    :
    :
    : It might also be fun to make a function that goes through the Morse-string and make the PC's buzzer to beep long or short depending on symbol.
    :
    Some usefull notatino is to between every word put / and between every letter put a space.

    If you want to 'buzz' it then have the time table is like this (if I recall corect)

    dot: one
    strike: two
    between letters: thre
    between words: four


    Happy coding wishes
    the one and only
    [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]


  • : : You don't have to make a table containing the letters, you could use the compiler's ASCII-table:
    : :
    : : char Morse[][6] = {".-","-...","-.-.",".....","--.."};
    : : char ch;
    : :
    : : scanf("%c", &ch);
    : :
    : : check that ch is a valid letter
    : :
    : : printf("%s
    ", Morse[ch-'A']);
    : :
    : :
    : : It might also be fun to make a function that goes through the Morse-string and make the PC's buzzer to beep long or short depending on symbol.
    : :
    : Some usefull notatino is to between every word put / and between every letter put a space.
    :
    : If you want to 'buzz' it then have the time table is like this (if I recall corect)
    :
    : dot: one
    : strike: two
    : between letters: thre
    : between words: four
    :
    :
    : Happy coding wishes
    : the one and only
    : [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]
    :
    :

    Thanks for the happy programming wishes. :)
    Though I am struggling with my assignment this time. But I guess Morse Code programming is a conventional assignment for the navices, hum?

    The code Lundin told me...
    [code]
    char Morse[][6] = {".-","-...","-.-.",".....","--.."};
    char ch;
    [/code]

    You said, ASCII-table. Ummm,,, don't know how.
    Above you used only 6 patterns of the Morse Code. Is that all we need? How do we.. for example get a word for example "Cat" from this?
    (Morse Code of 'C''A''T' is: -.-. .- - )

    I think you need a function to convert the English characters to Morse. That's the requirment of the assignment.

    Thanks!!!

  • [b][red]This message was edited by Lundin at 2006-3-1 0:1:42[/red][/b][hr]
    :
    : : : You don't have to make a table containing the letters, you could use the compiler's ASCII-table:
    : : :
    : : : char Morse[][6] = {".-","-...","-.-.",".....","--.."};
    : : : char ch;
    : : :
    : : : scanf("%c", &ch);
    : : :
    : : : check that ch is a valid letter
    : : :
    : : : printf("%s
    ", Morse[ch-'A']);
    : : :
    : : :
    : : : It might also be fun to make a function that goes through the Morse-string and make the PC's buzzer to beep long or short depending on symbol.
    : : :
    : : Some usefull notatino is to between every word put / and between every letter put a space.
    : :
    : : If you want to 'buzz' it then have the time table is like this (if I recall corect)
    : :
    : : dot: one
    : : strike: two
    : : between letters: thre
    : : between words: four
    : :
    : :
    : : Happy coding wishes
    : : the one and only
    : : [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]
    : :
    : :
    :
    : Thanks for the happy programming wishes. :)
    : Though I am struggling with my assignment this time. But I guess Morse Code programming is a conventional assignment for the navices, hum?
    :
    : The code Lundin told me...
    : [code]
    : char Morse[][6] = {".-","-...","-.-.",".....","--.."};
    : char ch;
    : [/code]
    :
    : You said, ASCII-table. Ummm,,, don't know how.
    : Above you used only 6 patterns of the Morse Code. Is that all we need? How do we.. for example get a word for example "Cat" from this?
    : (Morse Code of 'C''A''T' is: -.-. .- - )
    :
    : I think you need a function to convert the English characters to Morse. That's the requirment of the assignment.
    :
    : Thanks!!!
    :
    :


    This is the trick:

    [code]printf("%s
    ", Morse[ch-'A']);
    [/code]

    The ASCII symbols 'A' to 'Z' are placed adjacent in the ASCII-table, starting at decimal value 65 and increasing. By subtracting the value 65 or 'A' from the character, you will get an index between 0 and 25 corresponding to the morse code for that letter.

    And of course you need all morse patterns in the Morse-array.
  • Hi

    For all your advices, great thanks.
    But it's almost lingo to me..

    1. I don't know how I can assign A to the A's morse code, B to the B's morse code without typing all the sequences with using [b]switch[/b] like,

    [green]
    switch (text) {
    case'A': '.-'
    case'B': '-...'
    case'C': '-.-.'
    .
    .
    [/green]

    This is the simpliest way for my level to comprehend to be honest...
    but, I need to use the function [b]void convertToMorse (chat text[], char buffer[])[/b] which means.. the [b]switch[/b] doesn't work.. or does it? I need to convert the characters in the function then the function returns nothing (void), which also means I have to return the buffer pointer, is it?

    Can you or anyone give me more hints!!?

    Cheers!!

    x

    Oh, this is just... I tried...of course it doesn't execute! But I need something like this..besides, I can't submit the main-function part. Does it mean I have to initialise morse[]={ } in the convertToMorse function??
    [code]
    #include

    void convertToMorse(char* buffer, const char* text){
    int i;//for-counter

    //Only uppercase letters are valid
    for (i=0; i<*buffer; i++){
    if(text >= 'A' && text <= 'Z'){
    *buffer[text-'A']; //??? just borrowed Lundin's idea!
    } else if (text >= 'a' && text <= 'z') {
    printf("Invalid input. Please input only uppercase letters.
    ");
    continue;
    }/*end else-if*/
    }/*end for*/

    }/*end convertToMorse*/

    void main(){

    char text[];
    char buffer[26] = {'.-','-...','-.-.','.','..-','--.',//A-G
    '....','..','.---','-.-','.-..','--','-.',//H-N
    '---','.--.','--.-','.-.','...','-','..-',//O-U
    '...-','.--','-..-','-.--','--..'};//V-Z

    printf("Enter a line of characters:
    ");
    text = getchar();
    convertToMorse(text);


    }/*end main*/
    [/code]

  • : Hi
    :
    : For all your advices, great thanks.
    : But it's almost lingo to me..
    :
    : 1. I don't know how I can assign A to the A's morse code, B to the B's morse code without typing all the sequences with using [b]switch[/b] like,
    :
    : [green]
    : switch (text) {
    : case'A': '.-'
    : case'B': '-...'
    : case'C': '-.-.'
    : .
    : .
    : [/green]
    :
    : This is the simpliest way for my level to comprehend to be honest...
    : but, I need to use the function [b]void convertToMorse (chat text[], char buffer[])[/b] which means.. the [b]switch[/b] doesn't work.. or does it? I need to convert the characters in the function then the function returns nothing (void), which also means I have to return the buffer pointer, is it?
    :
    : Can you or anyone give me more hints!!?
    :
    : Cheers!!
    :
    : x
    :
    : Oh, this is just... I tried...of course it doesn't execute! But I need something like this..besides, I can't submit the main-function part. Does it mean I have to initialise morse[]={ } in the convertToMorse function??
    : [code]
    : #include
    :
    : void convertToMorse(char* buffer, const char* text){
    : int i;//for-counter
    :
    : //Only uppercase letters are valid
    : for (i=0; i<*buffer; i++){
    : if(text >= 'A' && text <= 'Z'){
    : *buffer[text-'A']; //??? just borrowed Lundin's idea!
    : } else if (text >= 'a' && text <= 'z') {
    : printf("Invalid input. Please input only uppercase letters.
    ");
    : continue;
    : }/*end else-if*/
    : }/*end for*/
    :
    : }/*end convertToMorse*/
    :
    : void main(){
    :
    : char text[];
    : char buffer[26] = {'.-','-...','-.-.','.','..-','--.',//A-G
    : '....','..','.---','-.-','.-..','--','-.',//H-N
    : '---','.--.','--.-','.-.','...','-','..-',//O-U
    : '...-','.--','-..-','-.--','--..'};//V-Z
    :
    : printf("Enter a line of characters:
    ");
    : text = getchar();
    : convertToMorse(text);
    :
    :
    : }/*end main*/
    : [/code]
    :
    :


    The code had a few errors, but you are making it more complicated than what it is. Also, the morse table seems to be incomplete.

    [code]
    #include <stdio.h>

    /* This function isn't really necessary, it just complicates things, imo. */
    const char* convertToMorse(char text, const char buffer[25][5])
    {

    if(text >= 'A' && text <= 'Z')
    return buffer[text - 'A'];
    else
    return NULL;
    }/*end convertToMorse*/

    int main(){

    char text;
    char buffer[25][5] = { ".-", "-...", "-.-.", ".", "..-", "--.",
    "....", "..", ".---", "-.-", ".-..", "--",
    "-.", "---", ".--.", "--.-", ".-.", "...",
    "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
    };

    printf("Enter a character:
    ");
    text = getchar();
    printf("%s
    ", convertToMorse(text, buffer));

    return 0;
    }/*end main*/
    [/code]
  • : The code had a few errors, but you are making it more complicated than what it is. Also, the morse table seems to be incomplete.
    :
    : [code]
    : #include
    :
    : /* This function isn't really necessary, it just complicates things, imo. */
    : const char* convertToMorse(char text, const char buffer[25][5])
    : {
    :
    : if(text >= 'A' && text <= 'Z')
    : return buffer[text - 'A'];
    : else
    : return NULL;
    : }/*end convertToMorse*/
    :
    : int main(){
    :
    : char text;
    : char buffer[25][5] = { ".-", "-...", "-.-.", ".", "..-", "--.",
    : "....", "..", ".---", "-.-", ".-..", "--",
    : "-.", "---", ".--.", "--.-", ".-.", "...",
    : "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
    : };
    :
    : printf("Enter a character:
    ");
    : text = getchar();
    : printf("%s
    ", convertToMorse(text, buffer));
    :
    : return 0;
    : }/*end main*/
    : [/code]
    :

    Lundin has given you the func to convert.
    Here a little more described:
    [code]
    char[] convertToMorse(char text, char[][] buffer){
    int i = text>='a' ? text-'a' : text-'A';
    //this line make i contain 0, if text contain A or a, 1 if text contains B or b ...
    return buffer[i];
    //index 0 of buffer contains the morsecode for A, 1 contains B ...
    }
    [/code]

    If you don't know ascii, then don't care about the subtraction thingy...
    You can learn it later.
  • To tokoG: if the following post is confusing, just ignore it :-)



    This is -not- ANSI C:

    char[] convertToMorse(char text, char[][] buffer){

    You could pass a pointer-to-pointer by typing char** buffer, but then it would only work with dynamicly allocated 2-d arrays.


    The fail-safe version of the same function, working with lower case letters:

    [code]
    #include

    const char* convertToMorse(char text, const char buffer[25][5])
    {
    if(isalpha(text))
    return isupper(text) ? buffer[text-'A'] : buffer[text-'a'];
    else
    return NULL;
    }/*end convertToMorse*/
    [/code]



  • : To tokoG: if the following post is confusing, just ignore it :-)
    :
    :
    :
    : This is -not- ANSI C:
    :
    : char[] convertToMorse(char text, char[][] buffer){
    :
    : You could pass a pointer-to-pointer by typing char** buffer, but then it would only work with dynamicly allocated 2-d arrays.
    :
    :
    : The fail-safe version of the same function, working with lower case letters:
    :
    : [code]
    : #include
    :
    : const char* convertToMorse(char text, const char buffer[25][5])
    : {
    : if(isalpha(text))
    : return isupper(text) ? buffer[text-'A'] : buffer[text-'a'];
    : else
    : return NULL;
    : }/*end convertToMorse*/
    : [/code]
    :
    OK, I'm don't know C/C++, I will never compile any of my bad code...

    But that thaught me a lot of new things...
    You can have a predefined buffer size, and you can't return an array.

    Thanks

    What does isalpha() do?
  • : : To tokoG: if the following post is confusing, just ignore it :-)
    : :
    : :
    : :
    : : This is -not- ANSI C:
    : :
    : : char[] convertToMorse(char text, char[][] buffer){
    : :
    : : You could pass a pointer-to-pointer by typing char** buffer, but then it would only work with dynamicly allocated 2-d arrays.
    : :
    : :
    : : The fail-safe version of the same function, working with lower case letters:
    : :
    : : [code]
    : : #include
    : :
    : : const char* convertToMorse(char text, const char buffer[25][5])
    : : {
    : : if(isalpha(text))
    : : return isupper(text) ? buffer[text-'A'] : buffer[text-'a'];
    : : else
    : : return NULL;
    : : }/*end convertToMorse*/
    : : [/code]
    : :
    : OK, I'm don't know C/C++, I will never compile any of my bad code...
    :
    : But that thaught me a lot of new things...
    : You can have a predefined buffer size, and you can't return an array.
    :
    : Thanks
    :
    : What does isalpha() do?
    :
    isalpha() just tells you if the character you enter is an alphanumerical character (0-9, a-z, A-Z), or possibly just alphabetic character (a-z, A-Z), can't remember which, but basically it tells you if the character is or isn't a letter, things like =,|,{,] shouldn't be returned as a character. Hopefully I remember that, I haven't been programming much lately, and forget things constantly, so please correct me if I'm wrong.
  • Hi Lundin and IDK

    PS: IDK, the deffinition of [italic][b]isalpha[/b][/italic] you asked according to my tutorial book is like this; Returns true if [italic]isupper[/italic] or [italic]islower[/italic] is true. :)


    Thanks for your replies.
    I need to ask something more.. sorry, this must sound hideous to you for Lundin been programming for [b]10 years![/b] (bowing bowing) I only started last year October and this is my second assignment. So,, the level is really low.

    [blue]
    I need to make everything happen in the function [b]void convertToMorse[/b] [red]except[/red] to print the result.
    So this function needs to include mapping of 'A'-'Z' convert in Morse code. The function has to store the result of the convertion to the parameter buffer.[/blue]

    - The text is a line, so not only one character but the whole text to be converted
    - Only uppercase to be handled
    - Ignore the spaces so I tried using the same logic Lundin showed me for [blue]trimming the spaces[/blue]

    Then... something like this came up with my idea... ah, hopeless...but,is the idea OK? I still need to ask about using ASCII thing

    [code]
    return buffer[text - 'A']
    [/code]

    But I will ask later... let me just get the right code first...

    [code]

    #include

    const char* convertToMorse(char text, const char buffer[25][5])
    {
    int i;

    [blue]
    //Since I need to get everything done in this function, besides, as for return value
    //this fanction should not have it. The function needs to [red][b]store[/b][/red] the result
    //of the conversion to the [red][b]parameter array buffer[/b][/red]

    //I asked this point to my tutors they said, I need to
    //[red][b]define mapping of 'A'-'Z' in this function[/b][/red]
    //otherwise I can't [b]convert letters in text[][/b] to morse codes [b]in buffer[][/b].

    //so... something like this comes here...?
    //can we not use 2D as it's bit complicated for me.. [/blue]

    char text[];
    char buffer[25][5] = { ".-", "-...", "-.-.", ".", "..-", "--.",
    "....", "..", ".---", "-.-", ".-..", "--",
    "-.", "---", ".--.", "--.-", ".-.", "...",
    "-", "..-", "...-", ".--", "-..-", "-.--", "--.."

    do
    {

    if(isupper(text))//If uppercase alphabet then, convert to morse
    for(buffer =0; buffer <= text; text++){//I want to convert the whole text, not just a character
    buffer[text - 'A']++
    }/*end for*/

    else {
    while(!text[i] >='A' && text[i] <='Z')
    text++; [red]//I need to ignore non-uppercase of everything else. [/red]
    }/*end else*/
    }while(buffer++ != '') //To loop until the end of buffer


    }/*end convertToMorse*/


    [blue][b]
    ///PLEASE IGNORE THE MAIN SINCE I CAN'T SUBMIT THE MAIN PART!
    //BUT IT WOULD BE GREAT IF YOU COULD CORRECT ME! :) -[/b]
    int main(){
    char buffer[26], text[];

    printf("Enter a text:
    ");
    text = getchar();
    convertToMorse(buffer, text);

    printf("%s
    ", buffer);

    return 0;
    system("PAUSE");
    }/*end main*/
    [/code][/blue


  • 10 posts reached.


    You can -never- return a pointer to a local variable from a function.
    If you are going to do everything from inside the function, you have to make the Morse-table global - then it is alright to return a pointer to it. The best in this case is probably to keep it local and copy the morse symbols from it into a second string.

    Your code doesn't make much sence... do you wish to convert a whole string of characters to morse code? In that case you need to make "text" a pointer.

    Sence you aren't allowed to print the result from inside the function, you must build a new string containing the whole result, like we did in that trim function. Leave the allocation to the caller (or use dynamic allocation).

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories