Morse Code: An alphabet or code in which letters are represented by combinations of long(dah) and short(dit) light or sound signals.
Note: This program does not include Morse Code for numberic characters.
Problem Statement:some researchers define an encoding scheme using an implementation of morse code. This scheme follows the following rule:A = ._ N = _. B = _... O = ___ C = _._. P = .__. D = _.. Q = __._ E = . R = ._. F = .._. S = ... G = __. T = _ H = .... U = .._ I = .. V = ..._ J = .___ W = .__ K = _._ X = _.._ L = ._.. Y = _.__ M = __ Z = __..For example, ".__..__._..._" is the word "PANDA". Unfortunately, this sequence also spells 'ADGBT', 'PAKV', 'WUNBT', 'WUNNIT', 'WUNNU', etc., along with many others. To solve this problem, your friend asks you to find a solution while he works on developing his own programming language.InputThe input will be a string of morse code.OutputAs output, print all of the possible translations, separated by new lines, in alphabetical order.ExampleSTDIN.__..__._..._STDOUT:ADGBT ADGDA ..... ADGNEET ... WUTLET .... WUTRIT WUTRU"..." shows there are more such words in between
Solution:
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> char buffer[26]; int l=0; char *Morse[26]; //initializing Morse Code array void initMorse(){ Morse[0] = "._" ; Morse[1] = "_..."; Morse[2] = "_._." ; Morse[3] = "_.." ; Morse[4] = "." ; //E Morse[5] = ".._." ; Morse[6] = "__." ; Morse[7] = "...." ; //H Morse[8] = ".." ; //I Morse[9] = ".___" ; //J Morse[10] = "_._" ; //K Morse[11] = "._.." ; Morse[12] = "__" ; //M Morse[13] = "_." ; Morse[14] = "___" ; //O Morse[15] = ".__." ; //P Morse[16] = "__._" ; Morse[17] = "._." ; //R Morse[18] = "..." ; Morse[19] = "_" ; Morse[20] = ".._" ; Morse[21] = "..._" ; //V Morse[22] = ".__" ; Morse[23] = "_.._" ; Morse[24] = "_.__" ; Morse[25] = "__.." ; //Z } int solution(char *s,int strt,char **Morse,int len){ int i,j,noMatch=0,k,prev,tem; int mlen; if(strt!=len) for(i=0;i<26;i++){ mlen=strlen(Morse[i]); if(strt+mlen<=len){ for(j=strt,k=0;j<strt+mlen&&k<mlen;j++,k++){ if(Morse[i][k]==s[j]) continue; else { noMatch=1; break; } } } else{ continue; } if(noMatch==0){ //print pattern when complete string matched if(strt+mlen==len){ buffer[l]=i+65; printf("%s\n",buffer); buffer[l]=0; } else{ noMatch=0; buffer[l]=i+65; l++; solution(s,strt+mlen,Morse,len); l--; // while backtracking buffer[l]=0; // clearing buffer just upto the previous location } } else{ noMatch=0; } } else{ buffer[l]=0; } return 1; } int main() { char s[100]; printf("Enter the input string of Morse code:\n"); scanf("%s",s); initMorse(); printf("Possible translations are:\n"); solution(s,0,Morse,strlen(s)); for return 0; }
Sample Output :
No comments:
Post a Comment