This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorse_convert.c
118 lines (108 loc) · 2.8 KB
/
morse_convert.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#include <stdlib.h>
/**
* Convert a letter (given as a C char) to a morse string of "."s and "-"s.
*
* @param char letter The letter to convert into a morse string.
* @return A string of dashes and periods representing the letter passed.
*/
char* convert(char letter) {
// As tempting as it is to convert this list into two columns: don't.
// Doing so will make our diffs and patches look like crap.
switch (letter) {
case 'a': return ".-";
case 'b': return "-...";
case 'c': return "-.-.";
case 'd': return "-..";
case 'e': return ".";
case 'f': return "..-.";
case 'g': return "--.";
case 'h': return "....";
case 'i': return "..";
case 'j': return ".---";
case 'k': return "-.-";
case 'l': return ".-..";
case 'm': return "--";
case 'n': return "-.";
case 'o': return "---";
case 'p': return ".--.";
case 'q': return "--.-";
case 'r': return ".-.";
case 's': return "...";
case 't': return "-";
case 'u': return "..-";
case 'v': return "...-";
case 'w': return ".--";
case 'x': return "-..-";
case 'y': return "-.--";
case 'z': return "--..";
case '1': return ".----";
case '2': return "..---";
case '3': return "...--";
case '4': return "....-";
case '5': return ".....";
case '6': return "-....";
case '7': return "--...";
case '8': return "---..";
case '9': return "----.";
case '0': return "-----";
case '?': return "..--..";
case '.': return ".-.-.-";
case ',': return "--..--";
case '-': return "-....-";
case '/': return "-..-.";
default:
printf("You passed a character that is not convertable. Whoops!\n");
printf("Acceptable characters are A-Z, 0-9, , . / ?\n");
printf("Please try again.\n");
exit(1);
}
}
void blink_short() {
printf("dit ");
}
void blink_long() {
printf("dah ");
}
/**
* Iterate through a string of text, and call the blink functions to represent
* each dash or dot.
*
* @param char* text The text to blink
*/
void perform_blink(char* text) {
// For each letter in the text, ...
while (*text) {
// Handle special cases here.
if (*text == ' ') {
printf("\n");
text++;
continue;
}
// Print the letter first.
printf("%c: ", *text);
// Convert the letter to morse, ...
char* morseified = convert(*text);
// For each char of the morse string, ...
while (*morseified) {
if (*morseified == '.') {
blink_short();
} else {
blink_long();
}
morseified++;
}
// For debugging...
printf("\n");
text++;
}
}
int main(int argc, char* argv[]) {
if (argc > 1) {
perform_blink(argv[1]);
return 0;
} else {
printf("Usage: ./morse 'text to enmorsify'\n");
return 1;
}
}