Question

In: Computer Science

There is a C function decodeMorse(const String & string, char message[]). This function examines the binary...

There is a C function decodeMorse(const String & string, char message[]). This function examines the binary string and iteratively constructs a decimal value (val) and width of each binary pattern (separated by spaces), until a space or a null character ('\0') is encountered in the string. Once a space or a null character is found, this function should call the assembly code (decode_morse()) to obtain the corresponding ASCII value, for the current val and width, and place the ASCII value in the message array, before resetting val and width for the next binary pattern (if any).

Solutions

Expert Solution

//Function to convert Morse code to text
char morsecode_to_text(char input[], int index){

int index_1 = 0, index_2 = 0, x = 0;
char output[100] = {'\0'};//Variable to keep output and later used to display text
char string[50] = {'\0'};//Variable to combine output

char *characters[] = {"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", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};


char *morsecode[] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---", "-.-",".-..","--","-.","---",".--.","--.-",
                     ".-.","...","-","..-", "...-",".--","-..-","-.--","--..", ".----","..---","...--","....-", ".....", "-....",
                     "--...","---..","----.","-----"};


while(input[index] < 1000){
    if(input[index] != '\0'){
        if(input[index] == '.' || input[index] == '-') {
            //Compare if input Morse code is similar to Morse code in array
            while(input[index] == '.' || input[index] == '-'){
                string[index_1] = input[index];
                index += 1;
                ++index_1;
            }//end of while loop

            for(index_2=0;index_2<36;index_2++){
                if(strcmp(string, morsecode[index_2]) == 0){
                    strcat(output, characters[index_2]);
                    x = 1;}
            }//end of for loop

        memset(string,0,sizeof(string));
        //Check for spaces
        }else if(input[index] == ' '){
            if(input[index + 1] == ' ') {
                if((input[index + 2] == ' ') && (input[index + 3] == ' ')){
                    strcat(output," ");
                    index += 4;
                }else{
                    strcpy(output, "FAIL - WRONG MORSE CODE");
                    break;}
            }else
                index++;

        }else if(input[index] == '\0' || input[index] == '\n'|| input[index] != '.' || input[index] != '-'){
            break;}
    }else
        break;
}//end of while loop

if (x == 1){
printf("MORSE CODE TRANSLATED : SUCCESS - %s", output);
}else{
    strcpy(output,"FAIL - WRONG MORSE CODE");
    printf("%s", output);
}

}//end of Function to convert Morse code to text

void main()
{
    char input[1000] = {'\0'}; //User input
    char choice = 'y'; //Amount of time user wants to replay
    int index = 0;

do{
    system("cls");
    printf("MORSE CODE TRANSLATOR PROGRAM\n");
    printf("\nPlease enter a Morse code string: ");
    fgets(input,1000,stdin);

    morsecode_to_text(input, index);

printf("\n\nDo you want to try again? (Press 'y' for Yes and 'n' for No): ");
scanf("%c", &choice);
getchar();
}while(choice == 'y');//end of while loop
}//end of void main

Related Solutions

Create a C module convertTo_csv.c that will implement the function loadAndConvert(const char* file) The code must...
Create a C module convertTo_csv.c that will implement the function loadAndConvert(const char* file) The code must be split into 2 files (.h file and a .c file). The convertTo_csv.c will have the function implementation in ti and the The convertTo_csv.h file will have the declaration. One argument will be given to the function, that is the name of the input file that needs to be converted. A function will create a new csv file called output.csv Know that the loadAndConvert...
C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state,...
C++ programming question class Address { public: Address(const std::string& street, const std::string& city, const std::string& state, const std::string& zip) : StreetNumber(street), CityName(city), StateName(state), ZipCode(zip) {} std::string GetStreetNumber() const { return StreetNumber; } void SetStreetNumber(const std::string& street) { StreetNumber = street; } std::string GetCity() const { return CityName; } void SetCity(const std::string& city) { CityName = city; } std::string GetState() const { return StateName; } void SetState(const std::string& state) { StateName = state; } std::string GetZipCode() const { return ZipCode; }...
Transfer in MIPS char * strtoupper(char s[]) { char c; c = s[0]; /* empty string...
Transfer in MIPS char * strtoupper(char s[]) { char c; c = s[0]; /* empty string */ if (c == 0) return s; /* convert the first character to upper case*/ if (c >= ‘a’ && d <= ‘z’) { c -= 32; s[0] = c; } /* convert the remaining characters*/ strtoupper(s + 1); return s; }
Translate the following C++ program to Pep/9 assembly language. const char chConst = '+'; char ch1;...
Translate the following C++ program to Pep/9 assembly language. const char chConst = '+'; char ch1; char ch2; int main() { cin.get(ch1); cin.get(ch2);    cout << ch1 << chConst << ch2;    return 0; }
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes...
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes the character x from string s. For s[] = “America”, a call to deleteSymbol(s, ‘a’) converts s[] = “Ame”
-Write in C++ -Use Char library functions Write a function that accepts a string representing password...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special character
I am trying to tokenize a string using a function by passing the char string[] and...
I am trying to tokenize a string using a function by passing the char string[] and char *pointer[100]. While I have working code inside the int main(), I am having trouble actually declaring the parameters for the function. I know how to pass the char array (char string[]), but not how to pass the char pointer array (char *pointer[100]). This is my code below: int main() {    // Declare variables    char str[] = "this is a test only...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates to string s with all occurrences of c removed. The string c is guaranteed to be a length-1 string; in other words a single character string. For example (remove-char "abc" "b") should evaluate to "ac". Here is pseudocode that you could implement.
C++ Modify FunctionTable.cpp so each that each function returns a string(instead of printing out a message)...
C++ Modify FunctionTable.cpp so each that each function returns a string(instead of printing out a message) and so that this value is printed inside of main(). //: C03:FunctionTable.cpp // Using an array of pointers to functions #include <iostream> using namespace std; // A macro to define dummy functions: #define DF(N) void N() { \ cout << "function " #N " called..." << endl; } DF(a); DF(b); DF(c); DF(d); DF(e); DF(f); DF(g); void (*func_table[])() = { a, b, c, d, e,...
#include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10...
#include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10 /*     This code closely mimics the Python hash table we created in class this     week. Each "person" is defined by their name, zipcode, and their pet's name.     Persons are hashed by their zipcode. */ //---------------------------------------------------------------------------- /* function declarations ------------------------*/ int computeKey(int); void add_to_buffer(string,int,string); void find_in_buffer(int); //---------------------------------------------------------------------------- /* define a "person" --------------------*/ class person{     public:     // variables that define a person     string name;     int zipcode;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT