|
|
|
@ -6,34 +6,44 @@
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
#define NUM_OF_LETTERS 36
|
|
|
|
|
#define NUM_OF_SYMBOLS 37
|
|
|
|
|
|
|
|
|
|
static char LOWERCASE_LETTERS[NUM_OF_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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
|
|
|
|
|
static char UPPERCASE_LETTERS[NUM_OF_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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
|
|
|
|
|
static const char LOWERCASE_LETTERS[NUM_OF_SYMBOLS] = {'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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' '};
|
|
|
|
|
static const char UPPERCASE_LETTERS[NUM_OF_SYMBOLS] = {'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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' '};
|
|
|
|
|
|
|
|
|
|
// 6 spaces are needed to store the null terminator :)
|
|
|
|
|
// C is fun?
|
|
|
|
|
static char MORSE[NUM_OF_LETTERS][6] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----"};
|
|
|
|
|
|
|
|
|
|
char* char_to_morse(char letter){
|
|
|
|
|
char result[6];
|
|
|
|
|
for (int i = 0; i < NUM_OF_LETTERS; i++){
|
|
|
|
|
static const char MORSE[NUM_OF_SYMBOLS][6] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----", "/"};
|
|
|
|
|
|
|
|
|
|
// This function returns a pointer to the morse code written above.
|
|
|
|
|
// The genuis of this function feels amazing because I've never used C before, but I see why
|
|
|
|
|
// ... it's used for performence!
|
|
|
|
|
//
|
|
|
|
|
// I do not copy ANY data. The pointer returned points to the above array!
|
|
|
|
|
const char* char_to_morse(char letter){
|
|
|
|
|
const char* result_ptr;
|
|
|
|
|
for (int i = 0; i < NUM_OF_SYMBOLS; i++){
|
|
|
|
|
if (letter == UPPERCASE_LETTERS[i] || letter == LOWERCASE_LETTERS[i]){
|
|
|
|
|
strcpy(&result, MORSE[i]);
|
|
|
|
|
//result = MORSE[i];
|
|
|
|
|
result_ptr = MORSE[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
return result_ptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char * to_morse(char* string){
|
|
|
|
|
char* string_to_morse(char* string){
|
|
|
|
|
int string_len = strlen(string);
|
|
|
|
|
// worse possible case is 6 times the length (assuming all numbers)
|
|
|
|
|
char* result;
|
|
|
|
|
for (int i = 0; i < strlen(string); i++){
|
|
|
|
|
// worse possible case is 8 times the length (assuming all numbers/punctuation, and adding spaces)
|
|
|
|
|
char result[string_len*8];
|
|
|
|
|
char* result_ptr = result;
|
|
|
|
|
// sets everything to null in the string, just in case there was data there previously.
|
|
|
|
|
strcpy(result, "");
|
|
|
|
|
for (int i = 0; i < string_len; i++){
|
|
|
|
|
strcat(result, char_to_morse(string[i]));
|
|
|
|
|
if (i != string_len-1){
|
|
|
|
|
strcat(result, " ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
return result_ptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *multi_tok(char *input, char *delimiter) {
|
|
|
|
@ -64,7 +74,7 @@ void print_single_morse(char charToMorse, bool isSlow, bool addLetter, long dotD
|
|
|
|
|
char convertedTo[6];
|
|
|
|
|
|
|
|
|
|
// Loop through every character in the list of letters
|
|
|
|
|
for (int j = 0; j < NUM_OF_LETTERS; j++){
|
|
|
|
|
for (int j = 0; j < NUM_OF_SYMBOLS; j++){
|
|
|
|
|
// If the character in the input string matches the character the character from the list
|
|
|
|
|
if (charToMorse == LOWERCASE_LETTERS[j] || charToMorse == UPPERCASE_LETTERS[j]){
|
|
|
|
|
strcpy(convertedTo, MORSE[j]);
|
|
|
|
@ -120,7 +130,7 @@ void reverse_morse(char* morseToNormalize){
|
|
|
|
|
char convertedChar;
|
|
|
|
|
|
|
|
|
|
// go through every morse encoding
|
|
|
|
|
for (int i = 0; i < NUM_OF_LETTERS; i++){
|
|
|
|
|
for (int i = 0; i < NUM_OF_SYMBOLS; i++){
|
|
|
|
|
// if the morse code is euqal to the input string
|
|
|
|
|
if (strcmp(MORSE[i], morseToNormalize) == 0){
|
|
|
|
|
isConvertable = true;
|
|
|
|
|