(Cryptograms) Write a program that creates a cryptogram out of a string. A cryptogram is a message or word in which each letter is replaced with another letter. For example the string The bird was named squawk might be scrambled to form cin vrjs otz ethns zxqtop Note that spaces are not scrambled. In this particular case, 'T' was replaced with 'x', each 'a' was replaced with 'h', etc. Uppercase
letters become lowercase letters in the cryptogram. Use tech- niques similar to those in Exercise 18.7.
What will be an ideal response?
```
// Program creates a cryptogram from a string.
#include
#include
#include
#include
using namespace std;
// prototype
void convertToLower( string::iterator, string::iterator );
int main()
{
string s;
string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string::iterator is;
string::iterator is2;
string::iterator is3;
srand( time( 0 ) ); // random generator
cout << "Enter a string: ";
getline( cin, s, '\n' ); // allow white space to be read
cout << "Original string: " << s;
is = s.begin(); // is points to the beginning of string s
// function convertToLower runs through the end
convertToLower( is, s.end() );
string s2( s ); // instantiate s2
is3 = s2.begin(); // is3 points to the beginning of s2
do
{
is2 = is3; // position location on string s2
// do not change spaces
if ( *is == ' ' )
{
++is;
continue;
} // end if
int x = rand() % alpha.length(); // pick letter
char c = alpha.at( x ); // get letter
alpha.erase( x, 1 ); // remove picked letter
// iterate along s2 doing replacement
while ( is2 != s2.end() )
{
if ( *is2 == *is )
*is2 = c;
++is2;
} // end while
++is3; // position to next element
++is; // position to next element
} while ( is != s.end() );
is3 = s2.begin();
convertToLower( is3, s2.end() ); // change s2 to lowercase
cout << "\nCryptogram of string: " << s2 << endl; // output string
} // end main
// convert strings to lowercase characters
void convertToLower( string::iterator i, string::iterator e )
{
// until the end is reached
while ( i != e )
{
*i = tolower( *i );
++i;
} // end while
} // end function convertToLower
```
Enter a string: a COLD hard Rain fell
Original string: a COLD hard Rain fell
Cryptogram of string: h fsjq thlq lhze dbjj
You might also like to view...
Parameterized stream manipulator setfill specifies the fill character that’s displayed when an output is displayed in a field wider than the number of characters or digits in the output. The effect of setfill applies:
a. Only to the current value being displayed. b. Only to outputs displayed in the current statement. c. Until explicitly set to a different setting. d. Until the output buffer is flushed.
Objects such as graphics and sound can be inserted into a Writer document
Indicate whether the statement is true or false
Consistency is one advantage of using a style provided by Word, as opposed to applying individual formatting to multiple sections of text
Indicate whether the statement is true or false
Biometric technology eliminates the need for a separate hardware device easily lost or damaged, and instead uses the increasingly available Trusted Platform Module (TPM) chip built into the computer or laptop device. The TPM chip is a hardware-level security device that provides all the security and encryption capabilities of a separate smart card device
Indicate whether the statement is true or false