Untitled

mail@pastecode.io avatarunknown
plain_text
a month ago
1.2 kB
1
Indexable
Never
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char morseToChar(const char* morse) {
    static const char* morseCodes[] = {
        ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
        "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
        "-.--", "--.."
    };

    static const char* alphabet = "abcdefghijklmnopqrstuvwxyz";

    for (int i = 0; i < 26; ++i) {
        if (strcmp(morse, morseCodes[i]) == 0) {
            return alphabet[i];
        }
    }

    return ' ';
}

void decodeMorseCode(const char* morseCode) {
    int morseLen = strlen(morseCode);
    char decoded[morseLen];
    int decodedIndex = 0;

    char* token = strtok((char*)morseCode, " ");
    while (token != NULL) {
        char letter = morseToChar(token);
        decoded[decodedIndex++] = letter;
        token = strtok(NULL, " ");
    }

    decoded[decodedIndex] = '\0';
    printf("%s\n", decoded);
}

int main() {
    char morseCode[1000];
    fgets(morseCode, sizeof(morseCode), stdin);
    morseCode[strcspn(morseCode, "\n")] = '\0';

    decodeMorseCode(morseCode);
    return 0;
}