0

I need to read a text file and change each character into morse code, then send it to another file.

is there an easier way than having 26 different arrays associated to a different letter?

3
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by Adak
0

AFAIK there is no algorithm to get morse code given an alphabet. So you can not avoid a maintaining this mapping.
But if you're using array(s) that's not so good. Use map.

0

Just realized I'm posting in C forum an not C++. :)
You have to use an array or char*(s). You can index into it using ascii code of the alphabet.
E.g.

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

#define INC 65

int main() {

    char** arr = malloc(sizeof(char*)*26);
    int i = 0;
    // You should populate the array one entry at a time, not in a loop like this.
    for(; i < 26; i++) {
        char* cstr = malloc(100);
        sprintf(cstr, "morsecode for %c\0",i+INC);
        arr[i] = cstr;
    }
    int j = 0;
    for(; j < 26; j++) {
        printf("%d, %c, %s\n", j+INC, j+INC, arr[j]);
    }
    return 0;
}
0

Morse code has no equation or algorithm to figure out it's series of dots and dashes.

It was made for brevity and ease of understanding, not computer algorithms, unfortunately for us. ;)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.