Contact Learn C
Copy

Program 247: Decryption of Morse Code Cipher in C

Program 247: Visit Morse Code encryption
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
    int i=0,j=0,k=0,flag=0;
    char *alphamorse[]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
                    "-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-",
     "...-",".--","-..-","-.--","--.."};
 char *nummorse[]={"-----",".----","..---","...--","....-",".....","-....","--...","---..","----."};
 
    char morseCode[1000]={0},substr[1000][100]={0},c;
    printf("Enter a Morse Code with spaces\n");
    gets(morseCode);    
    while(morseCode[k]!='\0')//for splitting sentence
    {
        j=0;
        while(morseCode[k]!=' '&&morseCode[k]!='\0')
        {
            substr[i][j]=morseCode[k];
            k++;
            j++;
        }
        substr[i][j]='\0';
        i++;
        if(morseCode[k]!='\0')
        {
            k++;
        }        
    }
    int len=i;
 for(i=0;i<len;i++)
 {
  for(j=0;j<25;j++)
  {
   if(!strcmp(alphamorse[j],substr[i]))//strcmp returns 0 if they are same
   {
    printf("%c",(j+65));
    break;
   }
  }
  for(j=0;j<9;j++)
  {
   if(!strcmp(nummorse[j],substr[i]))
   {
    printf("%d",j);
    break;
   }
  }  
 }
 printf("\n");
}
Explanation:
The above can also be done using switch case but you need to write a total of 35 cases which is not a good practice.

//Coming Soon
Output:
Decryption of Morse Code Cipher in C


Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide