Morse Code

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.3 kB
2
Indexable
Never
//Morse Code 

#include <stdio.h>
#include <string.h>

int n;
char s[1005], alp[20];

void morse_code(int cnt){
    //printf("%s\n", alp);
    char code[26][100] = {"=.===","===.=.=.=", "===.=.===.=", "===.=.=","=", "=.=.===.="
    , "===.===.=","=.=.=.=","=.=","=.===.===.==="
    ,"===.=.===","=.===.=.=","===.===","===.=","===.===.===","=.===.===.="
    ,"===.===.=.===","=.===.=","=.=.=","===","=.=.===","=.=.=.==="
    ,"=.===.===","===.=.=.===","===.=.===.===","===.===.=.="};
    for(int i = 0; i < 26; i++){
        if(strcmp(alp, code[i]) == 0){
            printf("%c", i+'A');
        }
    }
    memset(alp, '\0', 20);
}
int main(){

    scanf("%d", &n);
    scanf("%s", s);
    int cnt = 0, dotmp = 0;
    for(int i = 0; i < n; i++){

        alp[cnt] = s[i];
        cnt++;
        
        if(s[i] == '.') dotmp++;

        if(dotmp == 3 && s[i+1] != '.'){
            alp[cnt-3] = '\0';
            morse_code(cnt);
            cnt = 0, dotmp = 0;
        }
        else if(dotmp == 7){
            alp[cnt-7] = '\0';
            morse_code(cnt);
            if(i != n-1) printf(" ");
            dotmp = 0, cnt = 0;
        }
        else if(dotmp == 1 && s[i+1] != '.') dotmp = 0;
    
        if(i == n-1 && dotmp != 7) morse_code(cnt);
    }
    printf("\n");
}