| | |
Help needed with Caesar Cipher
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 7
Reputation:
Solved Threads: 0
Hi, I'm trying to write a code for Caesar Cipher, I don't seem to be able to get it right, I'm having no compiling errors, but there're some real logical errors, the encryption function is working fine, but I'm having troubles with the decryption.
I'd love it if someone helps me, but please if you can make the decryption function work the same way the encryption one works, I mean "mathematically".
thx very much in advance
I'd love it if someone helps me, but please if you can make the decryption function work the same way the encryption one works, I mean "mathematically".
thx very much in advance
C++ Syntax (Toggle Plain Text)
#include<iostream.h> #include<string.h> #include<windows.h> void CaesarEncrypt(char word[],int key,int size); void CaesarDecrypt(char word[],int key,int size); void GetWord(char word[],int &size); void PrintArray(char array[],int n); int main() { int key,size=-1; char word[100]; char choice; cout << "Enter the word please:\n"; GetWord(word,size); cout << "Please enter the key\n"; cin >>key; cout << "Enter \"e\" to encrypt, \"d\" to decrypt\n"; cin>>choice; switch (choice) { case 'E':case 'e':CaesarEncrypt(word,key,size);break; case 'D':case 'd':CaesarDecrypt(word,key,size);break; } PrintArray(word,size); return 0; } void CaesarEncrypt(char word[],int key,int size) { for (int i=0;i<size;i++) { if (word[i]>64&&word[i]<91) { word[i]=(char)(((word[i]+key-65)%26)+65); } if (word[i]<123&&word[i]>96) { word[i]=(char)(((word[i]+key-95)%26)+95); } } } void CaesarDecrypt(char word[],int key,int size) { for (int i=0;i<size;i++) { if (word[i]>64&&word[i]<91) { word[i]=(char)(((word[i]-key-65)%26)+65); } if (word[i]<123&&word[i]>96) { word[i]=(char)(((word[i]-key-95)%26)+95); } } } void GetWord(char word[],int &size) { do { size++; word[size]=cin.get();//cin.get() was added by Maya Shallouf } while (word[size]!='\n');//'\n' was added by Maya Shallouf } void PrintArray(char array[],int n) { for (int i=0;i<n;i++) { cout << array[i]; } cout <<endl; }
Last edited by Gewalop; Apr 23rd, 2009 at 1:29 pm.
•
•
Join Date: Apr 2009
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
I think you should send the argument "word" as a reference and not copies of the argument.
word is (as you see) an array, and the name "word" as a fixed pointer at the first item in the array ,so when you send it to the function , you do it like this.
C++ Syntax (Toggle Plain Text)
func(int array[]) { } int array[50]; func(array);
----------------------
People please... I need help
>>I think you should send the argument "word" as a reference and not copies of the argument.
He is passing a pointer actually.
>>the encryption function is working fine, but I'm having troubles with the decryption.
Even Encryption is not good. Here is the output:
So, what is the error?
I think you may have guessed.
Check if the Resultant character exceeds 'z' or 'Z' , If it does do the needfull
He is passing a pointer actually.
>>the encryption function is working fine, but I'm having troubles with the decryption.
Even Encryption is not good. Here is the output:
C++ Syntax (Toggle Plain Text)
siddhant3s@Xion:~/Documents$ ./testofcallbyreference Enter the word please: mathematically Please enter the key 5 Enter "e" to encrypt, "d" to decrypt e rf_mjrf_nhfqqd
So, what is the error?
I think you may have guessed.
Check if the Resultant character exceeds 'z' or 'Z' , If it does do the needfull
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Look, I coded the function myself and it is working great;
I will give you the code of the encrypter to get you started, you should write the decrypter yourself:
I will give you the code of the encrypter to get you started, you should write the decrypter yourself:
cpp Syntax (Toggle Plain Text)
void CaesarEncrypt(char word[],int key,int size) { for (int i=0;i<size;i++) { if (word[i] >='A' && word[i] <='Z') { int OV=word[i] + key;//overflow word[i]=(OV <= 'Z')? OV :'A' + (OV-'Z'-1); } if (word[i]<='z'&&word[i]>='a') { int OV=word[i] + key;//overflow word[i]=(OV <= 'z')? OV :'a' + (OV-'z'-1); } } }
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
![]() |
Other Threads in the C++ Forum
- Previous Thread: Need help implementing kd tree delete function
- Next Thread: UTF-8 in Dev-c++?
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






