QuestionQuestion

Write a program which reads A STREAM from the standard input, stores text until it encounters EOF, then encrypts the text using the Caesar Block Cipher.

(The program reads until it encounters eof, it doesn't just read one line.)

SHORT, SAMPLE RUN:

INPUT:
(karla.001:)
Karla: The young woman named Alexandra Borisovna Ostrakhova is your daughter.

a.out < karla.001

OUTPUT:
KonasaoeqauanokurbrnmdvhrnhlgernodwcawdaavaldToABOaurahmlosigbreaertshbzynxirytmo

PROOF OF PROGRAM CORRECTNESS:

a.out < karla.001 | a.out

OUTPUT:

KarlaTheyoungwomannamedAlexandraBorisovnaOstrakhovaisyourdaughternwlrbbmqbhcdarzo

----------------
Explanation:

It works like this:

Stage 1: User types:

Karla: The young woman named Alexandra Borisovna Ostrakhova is your daughter.

Stage 2: Remove all non-alpha chars // if (isalpha(c)) string s += c ;

Stage 3: Create a "square matrix" (an array of arrays of char with enough spaces to store the message.
a. store in String s then take s.length()
b. calculate the size of smallest perfect square that can hold message of that length
c. calculate: long is the side of that square? (what's the square root of s.length()?)
Don't use some math library function, PROGRAM THE COMPUTER! (What if you didn't HAVE a math library with a sqrt() function?)

int side = 0 ;
while (s.length() < (++side * side)) ;

(side is now the appropriate size of the side of a square.)

Stage 4: Allocate a two-dimensional array of char _that_ long on a side.

Stage 5:
Step through string s and place the letters in the array left to right, top to bottom ("Row Major Order" or rows first, then columns.)
(Use Nested for() loops)
(Pad empty elements at end of array with random alpha chars.)

Step 6:
Reverse the order of the for() loops, and pick off the characters, top to bottom, left to right, output them, a character at a time.

To decypher, do that very same thing with the encrypted msg. In other words, the program both encyphers and decyphers, symmetrically.

Example:

Stage 1:
this is sample text suitable
for a simulation of a diplomatic
mission or a spy's instructions

Stage 2:
thisissampletextsuitableforasimulationofadiplomaticmissionoraspysinstructions

Stage 3: The string's length() is 81.
Squares are: 2, 3, 9, 16, 25, 36, 49, 64, 81.
So 81 or 9 times 9, is the smallest table to hold the message.
(If the length of the message isn't a perfect square, we pick the next-largest square number and pad it with random characters.)
(NEEDS TO BE SAID: don't store a table of squares. That wastes space and cripples the program. CALCULATE squares using loop, and run it until your square is equal to or larger than the length of the string of input text.)
int side = 0 ;
while (s.length() > (++side * side)) ;

Stage 4:
char table[9][9] ; (EXAMPLE: your code will differ, and the literals will be replaced by a variable.)

Stage 5:
table holds:

thisistex
tsuitable
fortransl
ationinto
acipherto
demonstra
tethefunc
tioningof
thesystem

Stage 6:
Picking letters off from top to bottom, left to right, prints:

ttfaadttthsotceeihiuriimtoesitopohnsitrnhneiysaaiesfnstbnnrtugtelsttrnoexelooacfm

--------

tools:
std::string
string::length()
string::erase()
string::[]
STEPS TO A SOLUTION:

So: Read your message into a large buffer or a string object. String objects are more efficient and less dangerous, since they don't overflow like CStrings.

Remove the spaces and punctuation, make all letters all upper or all lower (it's harder for the enemy to break the code if you do).

Then count the chars in the message. (string::length())

Pick the first perfect square greater than the message length, allocate an array of char that size.

Read the message into a square array of that size from left to right, top to bottom.

IF THE MESSAGE DOESN'T FILL THE ARRAY COMPLETELY, INSERT RANDOM LETTERS AT THE END. (WRITE A RANDOM PRINTABLE-CHARACTER GENERATOR OR USE randchar.cpp.)

Learning outcomes:
Well, clearly, this teaches you how to process arrays with for() loops, plus knowing how arrays are saved in the computer and how to access their elements.

TEST: If you wrote a correct program, then this should output exactly the letters that are input (minus any spaces or punctuation):

$ cat file | caesarb | caesarb

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

string read(){
    string input = "";
    string result = "";
    string temp;      
    char c;
    while (cin>> temp) {      
       input += temp;
    }
   
    for (unsigned int i = 0; i < input.size(); i++) {
       c = input[i];
       if (isalpha(c)) {
            result += c;
       }
    }      
    return result;
}...
$15.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats