Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <iostream> #include <string> #include<cctype> using namespace std; int

ID: 3635087 • Letter: #

Question

#include <iostream>
#include <string>
#include<cctype>
using namespace std;

int main()
{char MORSE[40][7]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
"..-","...-",".--","-..-","-.--","--..",".----","..---","...--",
"....-",".....","-....","--...","---..","----.","-----"," ","--..--",".-.-.-","..--.."};
char in[100];
char EnglishAlphabet[40]={'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',' ',',','.','?'};
int i=0,j,k;
cout<<"Enter a string: ";
gets(in);
while(in[i]!='')
{for(j=0;j<40;j++)
{if(toupper(in[i])==EnglishAlphabet[j])
{for(k=0;k<strlen(MORSE[j]);k++)
cout<<MORSE[j][k];
cout<<" ";
}
}
i++;
}

cout<<endl<<endl;
system("pause");
return 0;
}

Explanation / Answer

Please Rate:Thanks


//header files declaration

#include <iostream>     //iostream needs to use input and output functions
#include <string>         //string needs to use all string functions like toUpper,gets
#include<cctype>        //cctype uses for charecter arrays
using namespace std;
//main function
int main()

//create an array MORSE with 40 rows and 7 columns it cintains the morse codes for alphabets and numbers
{char MORSE[40][7]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
"..-","...-",".--","-..-","-.--","--..",".----","..---","...--",
"....-",".....","-....","--...","---..","----.","-----"," ","--..--",".-.-.-","..--.."};

//declare a charecter array in with 100 size
char in[100];

//define a charecter array EnglishAlphabet stores all the alphabets and numbers rom 0 to 9 and symbols
char EnglishAlphabet[40]={'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',' ',',','.','?'};

//Declare variables used in iterators
int i=0,j,k;

//enter a string

cout<<"Enter a string: ";

//read the string into array in
gets(in);

//loop repeats till the last charecter in the string "in"
while(in[i]!='')

//repeats the for loop 40 times because there are 40 charecters in EnglishAlphabet array
{for(j=0;j<40;j++)

//compares the each charecter with in array to EnglishAlphabet array
{if(toupper(in[i])==EnglishAlphabet[j])

//if comaring result is true execute for loop
{for(k=0;k<strlen(MORSE[j]);k++)

//display the Morse code for the given string
cout<<MORSE[j][k];
cout<<" ";
}//end for loop
}//end for
i++; //update i with adding 1
}//end while

cout<<endl<<endl;
system("pause"); //command to stop the program
return 0; //return 0 to main function
}//end main