/** Version 3 of Morse Coder program. Create demorser * Author : E.S.Boese * Date : Fall 2006 * Revised: D.C.Matthews, Spring 2017 */ #include #include #include void morseIt(char line[]); void morseCode(char character); void demorseIt(char line[]); char getLetter(char word[]); int main() { char line[1024]; printf("Enter text to encode.\n"); fgets(line, sizeof(line), stdin); morseIt(line); printf("\n"); printf("Enter text to decode.\n"); fgets(line, sizeof(line), stdin); demorseIt(line); printf("\n"); return 0; } void demorseIt(char line[]) { #define MAX_WORD_LEN 5 char word[MAX_WORD_LEN]; int index=0; // index for word for (int i=0; line[i] != '\0'; i++) { if (line[i] == '#') // done with word { word[index] = '\0'; printf("%c", getLetter(word)); index = 0; // start over for next word } else word[index++] = line[i]; } } char getLetter(char word[]) { if (strcmp(word, ".-" ) == 0) return 'A'; if (strcmp(word, "-...") == 0) return 'B'; if (strcmp(word, "-.-.") == 0) return 'C'; if (strcmp(word, "-.." ) == 0) return 'D'; if (strcmp(word, "." ) == 0) return 'E'; if (strcmp(word, "..-.") == 0) return 'F'; if (strcmp(word, "--." ) == 0) return 'G'; if (strcmp(word, "....") == 0) return 'H'; if (strcmp(word, ".." ) == 0) return 'I'; if (strcmp(word, ".---") == 0) return 'J'; if (strcmp(word, "-.-" ) == 0) return 'K'; if (strcmp(word, ".-..") == 0) return 'L'; if (strcmp(word, "--" ) == 0) return 'M'; if (strcmp(word, "-." ) == 0) return 'N'; if (strcmp(word, "---" ) == 0) return 'O'; if (strcmp(word, ".--.") == 0) return 'P'; if (strcmp(word, "--.-") == 0) return 'Q'; if (strcmp(word, ".-." ) == 0) return 'R'; if (strcmp(word, "..." ) == 0) return 'S'; if (strcmp(word, "-" ) == 0) return 'T'; if (strcmp(word, "..-" ) == 0) return 'U'; if (strcmp(word, "...-") == 0) return 'V'; if (strcmp(word, ".--" ) == 0) return 'W'; if (strcmp(word, "-..-") == 0) return 'X'; if (strcmp(word, "-.--") == 0) return 'Y'; if (strcmp(word, "--..") == 0) return 'Z'; if (strcmp(word, "$$" ) == 0) return ' '; return word[0]; } void morseIt(char line[]) { for (int i=0; line[i] != '\0'; i++) { morseCode(line[i]); printf("#"); } } void morseCode(char character) { switch (toupper(character)) { case 'A': printf(".-"); break; case 'B': printf("-..."); break; case 'C': printf("-.-."); break; case 'D': printf("-.."); break; case 'E': printf("."); break; case 'F': printf("..-."); break; case 'G': printf("--."); break; case 'H': printf("...."); break; case 'I': printf(".."); break; case 'J': printf(".---"); break; case 'K': printf("-.-"); break; case 'L': printf(".-.."); break; case 'M': printf("--"); break; case 'N': printf("-."); break; case 'O': printf("---"); break; case 'P': printf(".--."); break; case 'Q': printf("--.-"); break; case 'R': printf(".-."); break; case 'S': printf("..."); break; case 'T': printf("-"); break; case 'U': printf("..-"); break; case 'V': printf("...-"); break; case 'W': printf(".--"); break; case 'X': printf("-..-"); break; case 'Y': printf("-.--"); break; case 'Z': printf("--.."); break; case ' ': printf("$$"); break; default: printf("%c", character); } }