GOT ITCode
* Description: Translates english to morse, and vice versa
* Input: English or Morse
* Output: Morse or English
* ************************************/
#include <iostream>
#include <string> // used only for toupper - no other functions are used elsewhere
using namespace std;
/********************************
* Prototypes
* *******************/
void engtomol (char*, const char*[]);
void moltoeng (char*, const char*[]);
char* input();
bool my_strcmp(char* one, char* two);
/**************************
* Function: Main
* Description: Runs program, gives options to user
* Parameters: None
* Return: 0;
* Pre: Char* arrays
* Post: English OR Morse text, printed to screen
* ***********************/
int main ()
{
const char* morse[26] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
char* text, *morsecode, choice, repeat='y';
while (repeat=='y')
{
cout << "Enter 1 to decode English to Morse\nEnter 2 to decode Morse code to English" << endl;
cin >> choice;
cin.ignore();
if (choice=='1')
{
cout << "Enter text to translate: ";
text = input();
cout << "MORSE CODE: " << endl;
engtomol(text, morse); cout << endl;
}
else if (choice=='2')
{
cout << "Enter your morsecode, space inbetween each letter. If multiple words, add 3 spaces after each word: ";
morsecode = input();
cout << "TEXT: " << endl;
moltoeng(morsecode, morse); cout << endl;
}
cout << "Would you like to try again? Enter y to repeat. Enter any other key to exit. ";
cin >> repeat;
cin.ignore();
}
return 0;
}
/************************
* Function: my_strlen
* Desc: Calculates length of user input
* Parameters: user input
* Return: Index
* Pre: Is the input valid char
* Post: Length of the string input
****************************/
int my_strlen(char* input)
{
int index=0;
while(input[index] != '\0')
{
index++;
}
return index;
}
char* resize(char* input, int& size);
/********************************
* Function: input
* Desc: Creates array for length of user input until \n (enter)
* Return: input size
* Parameters: None
* Pre: input is characters
* Post: Size of array of user input
********************************/
char* input()
{
char c;
char* input = '\0';
int size = 0;
while(true){
cin.get(c);
if(c == '\n')
{
break;
}
input = resize(input, size);
input[size-1] = c;
}
input = resize(input,size);
input[size-1] = '\0';
return input;
}
/******************
* Function: Resize
* Desc: Creates temp array to store user input, copies input length to temp
* Return: temp (array)
* Parameters: Input, size
* Pre: Valid input, valid size)
* Post: Temp array
* *********************/
char* resize(char* input, int& size)
{
char* temp = new char[size+1];
int i=0;
if(input !=NULL){
for(i=0; i < size; i++){
temp[i] = input[i];
}
delete [] input;
}
size++;
return temp;
}
/*******************
* Function: engtomol
* Desc: Converts english to morse code
* Parameters: text, morse[array]
* Return: N/A
* Pre: Valid char text
* Post: Converted text to morse
* ****************************/
void engtomol (char* text, const char* morse[])
{
int textlength = my_strlen(text);
const char* spacesbtwwords = " ";
int k=0;
for (int k=0; k<textlength; k++)
{
if (text[k]!= ' ') //if the word(s) did not encounter a space
{ text[k]=toupper(text[k]); //upper case letters and lower case letters are the same hence have the same appropriate morse code.
cout << morse[text[k]-'A'] << " ";
}
if (text[k]==' ')
{
cout << spacesbtwwords;
}
}
}
/**************************
* Function: moltoeng
* Desc: Converts morse to english text
* Return: N/A
* Parameters: morsecode, morse[array]
* Pre: Valid morse code
* Post: English text translated from morse
***********************/
void moltoeng(char* morsecode,const char* morse[])
{
char english[27] = {'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'};
int index = 0;
while(index < my_strlen(morsecode))
{
//find current morse code
char letter[6]; // morse code is 5 char long
int x = 0;
while(morsecode[index] == ' ')
{
x++;
index++;
if(x==3){
cout << ' ';
break;
}
}
int j=0;
while(morsecode[index] != ' ')
{
letter[j++] = morsecode[index++];
} //find this code in the morse alphabet
int position = 0;
for(position=0; position < 26; position++)
{
if(my_strcmp((char*) morse[position],letter))
{
cout << english[position];
++index;
break;
}
}
}
}
/*******************
* Function: my_strcmp
* Desc: Compares two string lengths to determine if same length
* Return: True or False
* Parameters: Two char*
* Pre: Valid char*
* Post: If not =, false. If =, true and continue with code
* ******************************/
bool my_strcmp(char* one, char* two)
{
int i= 0;
if(my_strlen(one) != my_strlen(two))
{
return false;
}
for(i=0; i < my_strlen(one); i++)
{
if(one[i] != two[i])
{
return false;
}
}
return true;
}