-
July 2nd, 2009, 06:51 PM
#1
C++ Caesar Cipher
Code:
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]) && (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[i]!='\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[i]!='\0')
cout<<transform(mess[i++],-key);
cout<<endl;
return;
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|