• 0

C++ Caesar Cipher


Question

I don't know if i did it right or not but can anyone help me with this code?

You must output:

HELLOWORLDYOUARECRAZY

mjqqtctwqietzfwjhwffe

THIS IS MY CODE:

#include <iostream>

#include <cstring>

#include <cctype>

using namespace std;

/*

* Description: This program operates on a command-line argument. It includes functions

* that implement a simple Caesar or Substitution cipher. Given key k and message m, the Caesar

* cipher will "shift" each letter in m k letters to the right using a standard english alphabet.

* The main program will take in a operation int, a key, and a message (either plaintext or

* encrypted. It will then output the message after it has been encrypted (operation 0) or

* decrypted (operation 1).

*/

// Function prototypes

void encrypt (int, char[]);

void decrypt (int, char[]);

char transform (char, int);

int main(int argc, char *argv[]) {

int n,key;

/*

* argv[0] is the name of the program

* argv[1] is the operation (encrypt 0 or decrypt 1)

* argv[2] is the key

* argv[3] is an array holding the message you want the program to operator on

*/

// First check to see if we received the correct number of arguments

// using argc. If not, print a "Usage" statement and return

if(argc != 4)

{

cout<<"Expected an integer and a string "<<endl;

return 0;

}

// Print original message supplied by user

cout<<"Print original message: "<<endl;

cout<<argv[3]<<endl;

// Convert the operation number and the numeric key to an integer

n=atoi(argv[1]);

key=atoi( argv[2]);

if(n==0)

encrypt(key,argv[3]);

else

decrypt(key,argv[3]);

// Call the appriopriate function based on the operation

// Print new message

cout<<"New message: "<<endl;

cin>>argv[3];

//system("PAUSE");

return 0;

}

/*

* Function name: transform

* Description: This function transforms (or shifts) <char> ch to another character

* <int> key letters away in the standard alphabet.

* Parameters:

* ch - a single character in the alphabet

* key - an integer that holds the number of shifts

* Return value: returns a "transformed" char

*/

char transform(char ch, int key)

{

// Array alpha contains the entire alphabet

char alpha[] = "abcdefghijklmnopqrstuvwxyz";

// Convert ch to lowercase so it can be matched to something in the alphabet

char low_case = tolower(ch);

// Loop until we locate the the letter or we reached until the end of the alphabet

int i=0;

while((low_case != alpha) && (i < 26))

i++;

// Handle the occurance when the message contains a character not in the alphabet

if (i > 25)

return ch;

// Handle the case where the key could be larger than the number of characters in the alphabet

int new_key = key % 25;

// If we have a letter at the end of the alphabet and the key takes us off the end

// then we need start at the beginning

if (new_key + i > 25)

{

return alpha[(new_key + i) % 25];

}

else

{

return alpha[new_key + i];

}

}

/*

* Function name: encrypt

* Description: This function takes plain text <char> message and a <int> key and executes

* the Caesar ciphter on the entire message using the transform function.

* Parameters:

* key - the number of shifts to completed the cipher

* message - the message to be encrypted

* Return value: none

*/

void encrypt(int key, char mess[])

{int i=0;

while(mess!='\0')

cout<<transform(mess[i++],key);

cout<<endl;

return;

}

/*

* Function name: decrypt

* Description: This function takes encrypted text <char> message and a <int> key and executes

* the Caesar ciphter on the entire message using the transform function.

* Parameters:

* key - the number of shifts to completed the cipher

* message - the message to be encrypted

* Return value: none

*/

void decrypt(int key, char mess[])

{int i=0;

while(mess!='\0')

cout<<transform(mess[i++],-key);

cout<<endl;

return;

}

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

What exactly seems to be the matter?

I ran it real quick and this is what I received.

Encode:

C:\~\Desktop&gt;ceasar.exe 0 5 HELLOWORLDYOUARECRAZY
Print original message:
HELLOWORLDYOUARECRAZY
mjqqtctwqietzfwjhwffe
New message:
1

C:\~\Desktop&gt;

Decode:

C:\~\Desktop&gt;ceasar.exe 1 5 mjqqtctwqietzfwjhwffe
Print original message:
mjqqtctwqietzfwjhwffe
hello orldeouarecraae
New message:
nil

C:\~\Desktop&gt;

Backwards Decode:

C:\~\Desktop&gt;ceasar.exe 1 -5 HELLOWORLDYOUARECRAZY
Print original message:
HELLOWORLDYOUARECRAZY
mjqqtctwqietzfwjhwffe
New message:
What is this for?

C:\~\Desktop&gt;

Backwards Encode:

C:\~\Desktop&gt;ceasar.exe 0 -5 mjqqtctwqietzfwjhwffe
Print original message:
mjqqtctwqietzfwjhwffe
hello orldeouarecraae
New message:
Is this needed?

C:\~\Desktop&gt;

Link to comment
Share on other sites

  • 0

can any tell me if this is right?

#include <iostream>

#include <cstring>

#include <cctype>

using namespace std;

/*

* Description: This program operates on a command-line argument. It includes functions

* that implement a simple Caesar or Substitution cipher. Given key k and message m, the Caesar

* cipher will "shift" each letter in m k letters to the right using a standard english alphabet.

* The main program will take in a operation int, a key, and a message (either plaintext or

* encrypted. It will then output the message after it has been encrypted (operation 0) or

* decrypted (operation 1).

*/

// Function prototypes

void encrypt (int, char[]);

void decrypt (int, char[]);

char transform (char, int);

int main(int argc, char *argv[])

{

int n,key;

/*

* argv[0] is the name of the program

* argv[1] is the operation (encrypt 0 or decrypt 1)

* argv[2] is the key

* argv[3] is an array holding the message you want the program to operator on

*/

// First check to see if we received the correct number of arguments

// using argc. If not, print a "Usage" statement and return

if(argc != 4)

{

cout<<"Expected an integer and a string "<<endl;

return 0;

}

// Print original message supplied by user

cout<<"Print original message: "<<endl;

cout<<argv[3]<<endl;

cout<<"New message: "<<endl;

// Convert the operation number and the numeric key to an integer

n=atoi(argv[1]);

key=atoi( argv[2]);

if(n==0)

encrypt(key,argv[3]);

else

decrypt(key,argv[3]);

//system("PAUSE");

return 0;

}

/*

* Function name: transform

* Description: This function transforms (or shifts) <char> ch to another character

* <int> key letters away in the standard alphabet.

* Parameters:

* ch - a single character in the alphabet

* key - an integer that holds the number of shifts

* Return value: returns a "transformed" char

*/

char transform(char ch, int key)

{

// Array alpha contains the entire alphabet

char alpha[] = "abcdefghijklmnopqrstuvwxyz";

// Convert ch to lowercase so it can be matched to something in the alphabet

char low_case = tolower(ch);

// Loop until we locate the the letter or we reached until the end of the alphabet

int i=0;

while((low_case != alpha) && (i < 26))

i++;

// Handle the occurance when the message contains a character not in the alphabet

if (i > 25)

return ch;

// Handle the case where the key could be larger than the number of characters in the alphabet

int new_key = key % 25;

// If we have a letter at the end of the alphabet and the key takes us off the end

// then we need start at the beginning

if (new_key + i > 25)

{

return alpha[(new_key + i) % 25];

}

else

{

return alpha[new_key + i];

}

}

/*

* Function name: encrypt

* Description: This function takes plain text <char> message and a <int> key and executes

* the Caesar ciphter on the entire message using the transform function.

* Parameters:

* key - the number of shifts to completed the cipher

* message - the message to be encrypted

* Return value: none

*/

void encrypt(int key, char mess[])

{

int i=0;

while(mess!='\0')

cout<<transform(mess[i++],key);

cout<<endl;

return;

}

/*

* Function name: decrypt

* Description: This function takes encrypted text <char> message and a <int> key and executes

* the Caesar ciphter on the entire message using the transform function.

* Parameters:

* key - the number of shifts to completed the cipher

* message - the message to be encrypted

* Return value: none

*/

void decrypt(int key, char mess[])

{

int i=0;

while(mess!='\0')

cout<<transform(mess[i++],-key);

cout<<endl;

return;

}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.