#include <iostream>
#include <string>
usingnamespace std;
constint SIZE = 10;
constchar BLANK = ' ';
string cipher( string text, bool encode )
{
if ( text.size() > SIZE * SIZE ) // Too big
{
cout << "Unable to encode/decode\n";
return text;
}
elseif ( text.size() < SIZE * SIZE ) // Pad to size
{
text += string( SIZE * SIZE - text.size(), BLANK );
}
string result( SIZE * SIZE, BLANK );
int pos = 0, cipherPos = 0;
int length;
for ( int diagonal = 0; diagonal < 2 * SIZE - 1; diagonal++ )
{
if ( diagonal < SIZE )
{
length = diagonal + 1;
pos = diagonal;
}
else
{
length = 2 * SIZE - 1 - diagonal;
pos = ( diagonal + 2 - SIZE ) * SIZE - 1;
}
for ( int l = 0; l < length; l++ )
{
if ( encode ) result[pos] = text[cipherPos];
else result[cipherPos] = text[pos];
// if ( encode ) result[cipherPos] = text[pos];
// else result[pos] = text[cipherPos];
pos += ( SIZE - 1 );
cipherPos++;
}
}
return result;
}
int main()
{
string test = "ONE OF THE EARLIEST KNOWN USES OF TRANSPOSITION ""CODES WAS DESIGNED BY THE SPARTANS";
cout << "Original: " << test << "\n";
test = cipher( test, true );
cout << "Encoded: " << test << "\n";
test = cipher( test, false );
cout << "Decoded: " << test << "\n";
}
Original: ONE OF THE EARLIEST KNOWN USES OF TRANSPOSITION CODES WAS DESIGNED BY THE SPARTANS
Encoded: ON INEAOEOTEEOSNNAFHASW S SEERTNOPC DEL FOOD AKU SDEBSN STIESYPS RTSI A I GTR WNHT
Decoded: ONE OF THE EARLIEST KNOWN USES OF TRANSPOSITION CODES WAS DESIGNED BY THE SPARTANS
Thank you so much. I haven't taken a look at your code yet as I still want to figure it out on my own (I learn the most this way), but I definitely will once my code works. This is what I have, but it keeps getting stuck. Transposition is meant to start at [0,0] and fill the diagonals and increase by 1 after each box. But the program keeps crashing, "main.exe not working". I changed the transposition function from my original post.
@Shezshade,
I can't follow all your code (my bad!) but I do suggest that you PUT SOME DEBUGGING LINES IN; it's more informative than the boolean operation of does it crash or doesn't it.
Please put the line cout << i+counter << " " << i-counter << " " << rows << " " << ones << endl;
BEFORE EACH of your current lines 69 and 80.
Then it will be clear to you where you go outside array bounds. Spectacularly.