(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...
When you do NOT specify a folder to search, and the file being search is stored in multiple locations, the Search Companion will locate ________ of the file or files that contain the search string
A) all copies B) only the non-music copies C) the first three D) no copies
A computer's NetBIOS name and hostname must be set to the same value
a. true b. false