1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include <iostream>
#include <string>
#include <stdio.h>
#include <ctype.h>
using namespace std;
//function prototypes
string morse(char m);
void englishMorse();
void morseEnglish();
string lowercase(string);
int main(){
cout << "Welcome to the Message Encryptor!" <<endl;
cout << "For English to Morse Code, press 1." << endl;
cout << "For Morse to English, press 2." << endl;
int a;
cin >> a;
if(a == 1){
englishMorse(); //passes to englishMorse function to encrypt message
}
else if(a == 2){
morseEnglish(); //passes to morseEnglish function to decrypt message
}
return 0;
}
//function with array for morse code
string morse(char m){
string alphabet = "abcdefghijklmnopqrstuvwxyz";
string morse[] = {"*-","-***","-*-*","-**","*","**-*","--*","****",
"**","*---","-*-","*-**","--","-*","---","*--*","--*-", "*-*","***",
"-","**-","***-","*--","-**-","-*--","--*"};
int finder = alphabet.find(m);
if(finder!=-1)
return morse[finder];
else
return "XX";
}
//function to convert user input into morse code
void englishMorse(){
cin.ignore();
cout << "Enter a message you'd like to encrypt :" << endl;
string message;
getline(cin, message);
string phrase = "";
lowercase(message);
for(int i=0; i<message.length(); i++){
if (i==0){
phrase = morse(message[i]) + "X";;
}
else {
phrase += morse(message[i]) + "X";
}
}
cout << phrase << endl;
}
//function to decrypt user's morse code
void morseEnglish(){
cin.ignore(); //to ignore everything around the string it is calling from getline
cout << "Enter the code you'd like to decrypt: " << endl;
string message;
getline(cin, message);
string phrase = "";
for(int i=0; i<message.length(); i++){
phrase += (message[i]) + "";
}
cout << phrase << endl;
}
//function to convert any user input to lower case
string lowercase (string message){
for(int x=0; x < message.length(); x++){
if(65 >= (int)message[x] or (int)message[x] <= 90) {
message[x] += 32;
}
}
return message;
}
|