Category Archives: Digital Portfolio

220 Digital Portfolio

 

Encode or Decode:

Creating a program that encode and decode could be frustrating at first, but once you get the idea it will be fun!

When I did this assignment I had to search for the ASCII table and take a deep look at it, and write down my thoughts about how to approach this program,

# include <iostream>
# include <sstream>
# include <string>
#include <locale>
# include <ctype.h>
using namespace std;
int main(){
string message;
string key;
int nkey;
string convert;
char input;
cout<<” You want to Encode or Decode?”<<endl;
cin>>input;
cin.ignore();
bool wrong;
if ((input != ‘e’ or input != ‘E’)&&( input != ‘d’ or input!= ‘D’)){ // this is to make sure that the user entered ‘e’ or ‘E’ or ‘d’ or’D’ to encode or decode
wrong = true; // if this the user entered other than that then it will return true for the wrong

 

 

printsforencode

decoded

}
else {
wrong = false;// here wrong will be false
}
while ( wrong == true){
if ((input == ‘e’ or input == ‘E’) or (input == ‘D’ or input == ‘d’)){
wrong = false;/// here to make sure if the program dedicated  the right keys
}
else {
cout<<”Please choose ‘E’ for encode OR ‘D for decode’”<<endl;

prentwrong
cin>>input;
cin.ignore();
}
}

while ( (input == ‘E’ or input == ‘e’) && (wrong == false)){
wrong = false;
cout<<”Enter the message you want to encoded here :” <<endl;
getline(cin,message);// get the message
cout << “Enter key”<<endl;
getline (cin,key);
cin.ignore();
for ( unsigned int b = 0 ; b <= message.size(); b++){
if (key.size()!= message.size()){
key += key[b]; // if the key is less then the message size then repeat that this will make sure that the key size is the same size as the message
}
}
for (unsigned int j= 0, h = 0 ; j < message.size(); j++,h++){ // loop through the message and get the number that represents each letter
nkey = key[h];
nkey = message[j] + key[h];
if (message[j] == ‘ ‘ ){
key[h--];
continue;
}
while (nkey > 126 ) {
nkey = nkey – 93;// to make sure that the numbers will stay in printable characters we do 126 -33
}
convert += static_cast<char>(nkey);

}
cout<<”The Encoded Message is – > “<<convert<<endl;
input++;
}

// so here if we enter the message “hello” to be encoded and use the keys “abc” it will result in

encodedhello

 

So hello rotated with the key abc will be ljrpt

while ((input == ‘d’ or input == ‘D’) && (wrong == false)){
wrong = false;
string dmessage;
string dkey;
int dnkey;
string dconvert;
cout<<”Enter decoded message here :” <<endl;
getline(cin,dmessage);
cin.ignore();
cout << “Enter key”<<endl;
getline (cin,dkey);
cin.ignore();
for ( unsigned int m = 0 ; m <= dmessage.size(); m++){
if (dkey.size()!= dmessage.size()){
dkey += dkey[m];
}
}
for (unsigned int o = 0, n= 0 ; o < dkey.size();o++,n++){
dnkey = dkey[n];
if (dmessage[o] == ‘ ‘){
dkey[n--];
continue;
}
dnkey = dmessage[o] – dkey[n];
while (dnkey < 33){
dnkey = dnkey + 93;
}
dconvert += static_cast<char>(dnkey);

}
cout<<”decoded text out of loop is – > “<<dconvert<<endl;
input++;

}

and then here is the decoded “hello” using the key “abc” we will be back

decodehelloman
return 0;

}