-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwith c.c
55 lines (51 loc) · 1.79 KB
/
with c.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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char* morseCode[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "/", NULL};
char* letters[] = {"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", " ", NULL};
char* convertToMorse(char* letter) {
char* morse = "";
int i, index;
for (i = 0; i < strlen(letter); i++) {
index = toupper(letter[i]) - 'A';
if (index < 0 || index > 26) {
continue;
}
morse = strcat(morse, morseCode[index]);
morse = strcat(morse, "/");
}
return morse;
}
char* convertToLetter(char* morse) {
char* letter = "";
char* code;
int i, j;
code = strtok(morse, "/");
while (code != NULL) {
for (i = 0; i < 27; i++) {
if (strcmp(code, morseCode[i]) == 0) {
letter = strcat(letter, letters[i]);
break;
}
}
code = strtok(NULL, "/");
}
return letter;
}
int main() {
char input[100];
printf("Enter 'm' to convert letter to Morse code or 'l' to convert Morse code to letter: ");
scanf("%s", input);
if (strcmp(input, "m") == 0) {
printf("Enter letter(s) to convert to Morse code: ");
scanf("%s", input);
printf("Morse code: %s\n", convertToMorse(input));
} else if (strcmp(input, "l") == 0) {
printf("Enter Morse code to convert to letter(s): ");
scanf("%s", input);
printf("Letter(s): %s\n", convertToLetter(input));
} else {
printf("Invalid input\n");
}
return 0;
}