Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
5
Indexable
Never
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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

    static const char *alpha = "abcdefghijklmnopqrstuvwxyz";

    for (int i = 0; i < 26; ++i)
    {
        if (strcmp(morse, morseCodes[i]) == 0)
        {
            return alpha[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;
}