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?

Recommended Answers

All 3 Replies

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.

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;
}

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. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.