#include <iostream> #include <cstdlib> #include <cstdio> #include <cctype> #incl
ID: 3821827 • Letter: #
Question
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <ctime>
#include <iomanip>
using namespace std;
int randchar(void)
{//rand char
int c = ' ';
while (!isalpha(c))
{
c = rand() * 1010110;
c %= 26;
c += 'a';
c %= 'z';
}
return c;
}//end rand char
int main(int argc, char * argv[], char **env)
{
char c;
string s;
c = cin.get();
while ( ! cin.eof()) //run until end of file
{
if(isalpha(c))
s =s + c;
c = cin.get();
int length = s.length(), side = 1, count=0, row, col;
while((side * side) < length) //start matrix as one by one and grow until side = string length
side++;
char matrix[side][side];
for(int row=0; row < side; row++) //increase row until row=side
{
for (int col=0; col < side; col++) //increase col until col=side
{
if (count < length) //if count is less than string length
{
matrix[row][col]=s[count++]; \fill 2-d matrix[row][col] with string until count = length
}//if ends
else
{
matrix[row][col] =randchar(); //if count = string length fill rest of array with random characterrs
count++;
} //else ends
}//for ends
}//for ends
for (int col=0; col < side; col++)
{
for (int row=0; row < side; row++)
cout << matrix[row][col];
}//for ends
}//while ends
} // END of main ()
This is a caesar block shift. A message is input into a square array and the empty elements are filled with random characters. The encryption is swapping the elements of its rows to its columns meaning the the input abcdefghi would be adgbehcfi after encryption. I'm having problems identifying why this source code is printing all the steps instead of only the elements of the final encrypted message. The correct output is printed after pressing enter so Im unsure if its a problem with some of my loops or a section of code I'm missing. Any help as to how to fix this would be greatly appreciated.
Thanks
Explanation / Answer
I have written a code which will show you the implementation of the Caesar Encryption and Decryption written differently
1. Encryption using Caesar E&D
int main()
{
char content[10000], choice;
int i, psswd;
printf("Content to encrypt: ");
gets(content);
printf("Enter the key which will be used to decrypt: ");
scanf("%d", &psswd);
for(i = 0; content[i] != ''; ++i){
choice = content[i];
if(choice >= 'a' && choice <= 'z'){
choice = choice + psswd;
if(choice > 'z'){
choice = choice - 'z' + 'a' - 1;
}
content[i] = choice;
}
else if(choice >= 'A' && choice <= 'Z'){
choice = choice + psswd;
if(choice > 'Z'){
choice = choice - 'Z' + 'A' - 1;
}
content[i] = choice;
}
}
printf("Displaying Encrypted content: %s", content);
return 0;
}
2. Decryption using Caesar E&D
#include<stdio.h>
int main()
{
char content[10000], choice;
int i, psswd;
printf("Enter a content to decrypt: ");
gets(content);
printf("Enter psswd: ");
scanf("%d", &psswd);
for(i = 0; content[i] != ''; ++i){
choiceoice = content[i];
if(choiceoice >= 'a' && choiceoice <= 'z'){
choiceoice = choiceoice - psswd;
if(choiceoice < 'a'){
choiceoice = choiceoice + 'z' - 'a' + 1;
}
content[i] = choiceoice;
}
else if(choiceoice >= 'A' && choiceoice <= 'Z'){
choiceoice = choiceoice - psswd;
if(choiceoice < 'A'){
choiceoice = choiceoice + 'Z' - 'A' + 1;
}
content[i] = choiceoice;
}
}
printf("Decrypted content: %s", content);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.