-
cryptogram program
Hi I am fairly new to C++ programming, but my professor has given us a project that I feel is beyond our scope of what we have learned.
The problem is to write a program that creates a cryptogram out of a string. Note that spaces are not scrambled. Uppercase letter should be treated the same as lowercase letters. Well this is the program that I wrote going with the example in our book, but he says that, it is completely wrong and that he wants a cryptogram program that asks for the input of a string and no matter what is entered it will encrypt it. So now I am completely lost and there is nothing in our book that goes beyond what my program does. Can any one help me?
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
int main()
{
//compiler concatenates all parts into one string
string s ( "see spot run");
cout <<"Original string before any replacements:\n"<<s
<<"\n\nAfter replacements:\n";
//replace all letters with other letters
int x = s.find("s");
while (x<string::npos) {
s.replace( x,1, "b");
x = s.find ("s", x+1);
int x = s.find ("e");
while (x<string::npos) {
s.replace(x,1, "j");
x = s.find ("e", x+1);
int x = s.find ("p");
while (x<string::npos){
s.replace(x,1,"m");
x = s.find ("p", x+1);
int x = s.find ("o");
while (x<string::npos){
s.replace(x,1,"z");
x = s.find ("o", x+1);
int x = s.find ("t");
while (x<string::npos){
s.replace(x,1,"k");
x = s.find ("t", x+1);
int x = s.find ("r");
while (x<string::npos){
s.replace(x,1, "q");
x = s.find ("r", x +1);
int x = s.find ("u");
while (x<string::npos){
s.replace(x,1, "c");
x = s.find ("u", x +1);
int x = s.find ("n");
while (x<string::npos){
s.replace(x,1, "a");
x = s.find ("n", x +1);
}
}
}
}
}
}
}
}
cout <<s<< endl;
return 0;
}
-
to your compiler characters and digits are just a series of ones and zeros arranged in a specific order. The firt level of abstraction arranges these ones and zeros into 16 or 32 bit patterns that represent numbers from 0 to 128 or 0 to 256 or some such orders. Each of thes numbers is then assigned a symbol such as the letter c or the * or the ] or the digit 9 or whatever according to a character set, usually ASCII but others are avaiable. Finally the characters are arranged into variables with may represent a string or a single char or an int or a float or a user defined class etc. The point is that everything you type into the program has an underlying numerical basis meaning you can manipulate the value into something that it's not and back again, that is chang the value so looking at the ouput doesn't make sense anymore. This process is called encryption. You can do it char by char in a manner you have attempted, but it's easier to a key that holds for each value of the message you are trying to encrypt or decrypt. You can use whatever key you want. For example you can increment the ASCII value of all char by a given amount, or you can shift bits, or whatever. The basic process is:
decide on a key;
obtain input;
adjust input using key;
readjust input using key back to original value;
cout << enter a string
determine length of string
use a loop to look at each char in string using isalpha() to determine if it is a letter.
if it is a letter use tolower() to change it to lower case letter (you might have to do this depending how you interpret what is intended as "handled same as lower case letters") and
then apply the key to ASCII value storing new value in string.
out put new resulting string when loop is done
now reverse process to get back original string.
Obviously the protocol for encryption can be much more complex, and there are probably simpler versions availabe, too. But I think this one should satisfy your instructor.