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; }...
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
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.
#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;...
c program //the file's size in bytes unsigned int fileSize(const char* name){     return 0; }...
c program //the file's size in bytes unsigned int fileSize(const char* name){     return 0; } //same as fileSize except if the file is a directory then it recursively finds the size of directory and all files it contains unsigned int fileSizeRec(const char* name){     return 0; }
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds...
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds the string 'f' in the string 'b' and replaces it with the string 't'. You can assume that f and t same length Example: char string[] = "zap";     replace(string, "ap", "oo"); --changes 'string' to "zoo".     *don't assume substring being replaced is singular, or that its own substrings are unique.     *Don't re scan letters already checked and replaced         char string[] =...
Write a method ( C++ ) map occurance_map(const string path); that reads in an ascii text...
Write a method ( C++ ) map occurance_map(const string path); that reads in an ascii text file and returns an assocation where each key is a word in the text file and each value is the number of occurances of that word. Ignore punctuation and numbers. The method should be case-insensitive and should store the keys as lowercase. A word is definited by an string consisting entirely of alpha-numeric characters or apostrophes (single quote characteris). For example, if the file...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT