| | |
Morse Code
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
This code uses a structure to map a letter to an equivalent representation of Morse code. It simply loops through either a string to encode or decode looking for text to substitute with a replacement string or character. Output is presented to the stdout.
#include <stdio.h> #include <string.h> #include <ctype.h> static const struct { const char letter, *morse; } Code[] = { { 'A', ".- " },{ 'B', "-... " },{ 'C', "-.-. " },{ 'D', "-.. " }, { 'E', ". " },{ 'F', "..-. " },{ 'G', "--. " },{ 'H', ".... " }, { 'I', ".. " },{ 'J', ".--- " },{ 'K', ".-.- " },{ 'L', ".-.. " }, { 'M', "-- " },{ 'N', "-. " },{ 'O', "--- " },{ 'P', ".--. " }, { 'Q', "--.- " },{ 'R', ".-. " },{ 'S', "... " },{ 'T', "- " }, { 'U', "..- " },{ 'V', "...- " },{ 'W', ".-- " },{ 'X', "-..- " }, { 'Y', "-.-- " },{ 'Z', "--.. " },{ ' ', " " }, }; void encode(const char *s) { size_t i, j; for ( i = 0; s[i]; ++i ) { for ( j = 0; j < sizeof Code / sizeof *Code; ++j ) { if ( toupper(s[i]) == Code[j].letter ) { printf("%s", Code[j].morse); break; } } } putchar('\n'); } void decode(const char *morse) { size_t i, j; for ( i = 0; morse[i]; ) { for ( j = 0; j < sizeof Code / sizeof *Code; ++j ) { size_t size = strlen(Code[j].morse); if ( memcmp(Code[j].morse, &morse[i], size) == 0 ) { putchar(Code[j].letter); i += size; break; } } } putchar('\n'); } int main(void) { const char text[] = "Hello world"; const char test[] = ".... . .-.. .-.. --- .-- --- .-. .-.. -.. "; encode(text); decode(test); return 0; } /* my output .... . .-.. .-.. --- .-- --- .-. .-.. -.. HELLO WORLD */
Similar Threads
- Guide me to write code for barcode scan with CODE 39 BRACODE font in VB.NET (VB.NET)
- Morse Code Generator (C++)
- How to change php code html code and code like this ? (Existing Scripts)
- making a inventory(rpg based)... problems[code] [/code] (C++)
- Can we Programaticall Write code in VB6.0, forms/standard module/class code window (Visual Basic 4 / 5 / 6)
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks bash binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql number open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming stack standard strchr string strings structures suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h



