Skip to content

Instantly share code, notes, and snippets.

@rsmahmud
Last active January 4, 2017 17:42
Show Gist options
  • Save rsmahmud/0a6f256fdf1ff94dff650e85d1f72ed3 to your computer and use it in GitHub Desktop.
Save rsmahmud/0a6f256fdf1ff94dff650e85d1f72ed3 to your computer and use it in GitHub Desktop.
Morse Code
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#define MAX 49
char alpha[MAX] ="ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789.,:?'-/()\"@=";
char *morse[MAX]={ ".-","-...","-.-.","-..",".","..-.","--.",
"....","..",".---","-.-",".-..","--","-.",
"---",".--.","--.-",".-.","...","-","..-",
"...-",".--","-..-","-.--","--..",
"/",
"-----",".----","..---","...--","....-",
".....","-....","--...","---..","----.",
".-.-.-","--..--","---...","..--..",
".----.","-....-","-..-.", "-.--.-",
"-.--.-",".-..-.",".--.-.","-...-"
};
void Help(){
puts("\n\t\t .-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-.");
puts("\t\t || ||");
puts("\t\t || Help - Morse Code Table ||");
puts("\t\t || Morse Code Value for Latin Alphabet ||");
puts("\t\t || Decimal Numbers & Punctuation Signs ||");
puts("\t\t || ||");
puts("\t\t *-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-*\n");
int i=0;
while(alpha[i++] != '\0'){
printf(" %02d. '%c' = %-6s ",i,alpha[i-1],morse[i-1]);
if ( (i)%4 == 0 ) printf("\n");
}
printf(" %02d. [ ] = [ Invalid Character / Code ]",i);
}
int Intro(){
int choice;
char c;
printf("\n");
puts("\t\t .-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-.");
puts("\t\t || ||");
puts("\t\t || Morse Code ||");
puts("\t\t || ||");
puts("\t\t ||~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~||");
puts("\t\t || ||");
puts("\t\t || 1. Encode [ Text - Morse Code ] ||");
puts("\t\t || 2. Decode [ Morse Code - Text ] ||");
puts("\t\t || 3. Help [ View Morse Code Table ] ||");
puts("\t\t || 0. Exit [ Exit Program ] ||");
puts("\t\t || ||");
puts("\t\t *-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-*");
printf("\n\t\t Enter Your Choice > ");
scanf("%d",&choice);
return choice;
}
void Morse(char c){
int i,flag=100;
for ( i=0; i<MAX; i++){
if ( c == alpha[i]){
flag = i;
break;
}
}
if ( c == 10)
printf("\n");
else
flag == 100 ? printf("[ ] ") : printf("%s ",morse[flag]);
}
void Encode(){
char ch,strin[2048];
int i=0;
fflush(stdin);
scanf(" %[^\n]",strin);
printf("\n\n Your Text Input : %s\n",strin);
printf("\n Morse Code Output : ");
strupr(strin);
i=0;
while(strin[i] != '\0'){
Morse(strin[i]);
i++;
}
printf("\n");
}
void Text(char *code){
int i,flag=100;
for ( i=0; i<MAX; i++){
if ( strcmp(code,morse[i]) == 0 ){
flag = i;
break;
}
}
if ( *code == 10)
printf("\n");
else
flag == 100 ? printf("[ ] ") : printf("%c",alpha[flag]);
}
void Decode(){
char ch,strin[4096], strin2D[800][7];
int i=0,j=0,k=0;
fflush(stdin);
scanf(" %[^\n]",strin);
for(i=0; strin[i]!='\0'; i++){
if(strin[i]==' '){
strin2D[j][k] = '\0';
k = 0;
j++;
}
else{
strin2D[j][k] = strin[i];
k++;
}
}
strin2D[j][k] = '\0';
printf("\n\n Your Morse Code Input : %s\n",strin);
printf("\n Plain Text Output : ");
//strupr(strin);
for(i=0; i <= j; i++){
Text(strin2D[i]);
}
printf("\n");
}
int main(){
system("title Morse Code");
system("mode 80,30");
do{
system("cls");
switch(Intro()){
case 1:
system("cls");
puts("\n Enter Text to Get Morse Code Output. [ Dot Dash Format ]\n");
//puts(" *** Important : type \"#\" at the end of your text input. ***");
puts(" *** Do not enter more than 2000 characters ***");
printf("\n\n Input : ");
Encode();
printf("\n\n\n Press any key to continue...");
break;
case 2:
system("cls");
puts("\n Enter Morse Code to Get Plain Text Output. [ Dot Dash Format ]\n");
//puts(" *** Important : type \"#\" at the end of your morse code input. ***");
puts(" *** Do not enter more than 4000 characters or more than 800 words ***");
printf("\n\n Input : ");
Decode();
printf("\n\n\n Press any key to continue...");
break;
case 3:
system("cls");
Help();
printf("\n\n\n Press any key to continue...");
break;
case 0:
printf("\n Thank You..!\n");
usleep(999999);
usleep(99999);
return 1;
default:
puts("\n\t\t Invalid Choice. Please Try Again.");
printf("\n\t\t Press any key to continue...");
}
fflush(stdin);
getch();
}while(1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment