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

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;...
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[] =...
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
in C programming language char character [100] = "hello"; a string array variable It is given....
in C programming language char character [100] = "hello"; a string array variable It is given. By writing a function called TranslateString, By accessing the pointer address of this given string, returning the string's address (pointer address) by reversing the string Write the function and use it on the main function. Function void will not be written as. Return value pointer address it will be. Sweat operation on the same variable (character) It will be made. Declaration of the function...
what is a const in C++? what does const mean in these functions
what is a const in C++?what does const mean in these functions-> " void idkwhattonameit( const name) {} "-> " void idkwhattonameit2( const &name) {} "-> " void idkwhattonameit3( const * name) {} "-> " void idkagain (const unique_ptr < Name > name_uPtr)"
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT