training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

morse.c (2486B)


      1 /* Exercise 8.39 */
      2 
      3 #include <stdio.h>
      4 #include <string.h>
      5 #include <ctype.h>
      6 
      7 #define SIZE 254
      8 #define CHARS 39
      9 
     10 void tomorse(char *string, char table[][CHARS][7]);
     11 void toeng(char *string, char table[][CHARS][7]);
     12 
     13 int main(void)
     14 {
     15    char string[SIZE] = { 0 };
     16    char table[2][CHARS][7] = {
     17 
     18       /* Morse code [A,Z] */
     19       { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
     20         ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
     21         "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
     22 
     23         /* Numbers [0,9] */
     24         "-----", ".----", "..---", "...--", "....-", ".....", "-....",
     25         "--...", "---..", "----.",
     26 
     27         /* Fullstop, comma and query */
     28 	".-.-.-", "--..--", "..--.." },
     29 
     30       /* English charset [A,Z] */
     31       { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
     32         "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
     33 
     34         /* Numbers [0,9] */
     35         "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
     36 
     37         /* Fullstop, comma and query */
     38 	".", ",", "*" }
     39    };
     40 
     41    printf("Give me an english word: ");
     42    gets(string);
     43    tomorse(string, table);
     44 
     45    printf("\nGive me a morse code: ");
     46    gets(string);
     47    toeng(string, table);
     48 
     49    printf("\n");
     50 
     51    return 0;
     52 } /* E0F main */
     53 
     54 /* Print a string converted to morse */
     55 void tomorse(char *string, char table[][CHARS][7])
     56 {
     57    int i, j, c;
     58    char *tokp;
     59 
     60    tokp = strtok(string, " ");
     61    while(tokp != NULL) {
     62       c = 0;
     63 
     64       for(i = 0; i < (int)strlen(tokp); i++) {
     65 	 for(j = 0; j < CHARS; j++) {
     66 	    if ( toupper((int)tokp[i]) == *table[1][j] ) {
     67 	       printf("%s", table[0][j]);
     68 	       c = 1;
     69 	       break;
     70 	    }
     71 	 }
     72 
     73          /* Unknown characters */
     74 	 if(!c)
     75 	    printf("?");
     76 
     77 	 printf(" ");
     78       }
     79 
     80       printf("   ");
     81       tokp = strtok(NULL, " ");
     82    }
     83 
     84 } /* eof toeng() */
     85 
     86 /* Print a string converted to english */
     87 void toeng(char *string, char table[][CHARS][7])
     88 {
     89    int i, c;
     90    char *tokp;
     91 
     92    tokp = strtok(string, " ");
     93    while(tokp != NULL) {
     94       c = 0;
     95 
     96       for(i = 0; i < CHARS; i++) {
     97 	 if( !(memcmp(tokp, table[0][i],
     98 	    strlen(tokp) > strlen(table[0][i]) ?
     99 	       strlen(tokp) : strlen(table[0][i]) )) ) {
    100 
    101 	    printf("%s", table[1][i]);
    102 	    c = 1;
    103 	    break;
    104 	 }
    105       }
    106 
    107       /* Unknown characters */
    108       if(!c)
    109 	 printf("?");
    110 
    111       if( !(strncmp((tokp + strlen(tokp) + 1), "  ", 2)) )
    112          printf(" ");
    113 
    114       tokp = strtok(NULL, " ");
    115    }
    116 
    117 } /* eof toeng() */
    118