for a project i need to encrypt a message and have the encrypted version be the next character in the alphabet (like secret would be tfdsfu) what code would i use for that?
Printable View
for a project i need to encrypt a message and have the encrypted version be the next character in the alphabet (like secret would be tfdsfu) what code would i use for that?
Well you are going to have to take every letter and change it with another letter, then store in an array/string and display it.
Begin with a function that encrypts it and it returns the ectrypted word.
this is what i have so far.
Code:#include <iostream>
using std::cout;
using std::endl;
void encrypt ( char e[] );
void decrypt ( char *ePtr );
int main()
{
// create a string to encrypt
char string[] = "this is a secret!";
cout << "Original string is: " << string << endl;
encrypt( string );
cout << "Encrypted string is: " << string << endl;
decrypt( string );
cout << "Decrypted string is: " << string << endl;
return 0;
} // end main
// encrypt data
void encrypt( char e[] )
{
( e += 1);
} // end function encrypt
// decrypt data
void decrypt( char *ePtr )
{
(*ePtr -= 1);
} // end function decrypt
The encrypt function is wrong simply because you've made it an array. The syntax is incorrect. (Well, arrays do degrade into a pointer, but it's ugly and the syntax is still wrong.) Also, both of those only modify one character. You need to loop through the entire string. Either make both functions loop, or make the two functions titled something more appropriate, like 'encryptChar' 'decryptChar', and call them repeatedly.
Also the parenthesis in both of those is not needed.
Quzah.
It's only modifying the first letter. How do i modify the function so that it will modify all of the letters?
Code:#include <iostream>
using std::cout;
using std::endl;
void encrypt ( char e[] );
void decrypt ( char *ePtr );
int main()
{
// create a string to encrypt
char string[] = "this is a secret!";
cout << "Original string is: " << string << endl;
encrypt( string );
cout << "Encrypted string is: " << string << endl;
decrypt( string );
cout << "Decrypted string is: " << string << endl;
return 0;
} // end main
// encrypt data
void encrypt( char e[] )
{
*e += 1;
} // end function encrypt
// decrypt data
void decrypt( char *ePtr )
{
*ePtr -= 1;
} // end function decrypt
Do you know how to loop through all of the characters in a string? If not, that's what you need to figure out first. Also, you shouldn't name your variables 'string', because C++ has a string class, which oddly enough is called string.
Here's a hint:
All strings are terminated with a null character (the '\0'). So, you can simply loop through the string until you find it. When you do, you're at the end and can stop.
Quzah.
how do you loop through all characters in a string?
>how do you loop through all characters in a string?
Here's an example.
Code:char message[] = "this is a secret!";
for (i=0; message[i] != '\0'; ++i)
cout << message[i];
cout << endl
use an index to keep track of which char of the string you are evaluating. Start the evaluation at index = 0. Discontinue the loop when value of current char is '\0' or when index is equal to length of string, whichever you prefer. Remember to add the terminating null char at index equal to the length of original string in the encrypted version so that the encrypted version is a string and not a simple char array. You don't want to encrypt the null char. If you donn't know how to make a loop out of this description I'd refer you to the tutorial for a discussion of the various options available.
The last issue you will need to decide is how to handle z. Do you change it to an a or to whatever comes after z in the ASCII table?
I'm still having trouble. The bolded parts is what i think is wrong. What do i need to do to them?
Code:#include <iostream>
using std::cout;
using std::endl;
void encrypt ( char e[] );
void decrypt ( char *ePtr );
int main()
{
// create a string to encrypt
char string[] = "this is a secret!";
cout << "Original string is: " << string << endl;
encrypt( string );
cout << "Encrypted string is: " << string << endl;
decrypt( string );
cout << "Decrypted string is: " << string << endl;
return 0;
} // end main
// encrypt data
void encrypt( char e[] )
{
int i;
for (i=0; i<strlen(e); i++ );
{
(e[i] +=1);
}
} // end function encrypt
// decrypt data
void decrypt( char *ePtr )
{
ePtr [0] -= 1;
ePtr [1] -= 1;
ePtr [2] -= 1;
ePtr [3] -= 1;
ePtr [4] -= 1;
ePtr [5] -= 1;
ePtr [6] -= 1;
ePtr [7] -= 1;
ePtr [8] -= 1;
ePtr [9] -= 1;
ePtr [10] -= 1;
ePtr [11] -= 1;
ePtr [12] -= 1;
ePtr [13] -= 1;
ePtr [14] -= 1;
ePtr [15] -= 1;
ePtr [16] -= 1;
} // end function decrypt
This semicolon makes the for loop an empty loop. You want to remove that semicolon.Quote:
Originally Posted by fenixataris182
Also, realize that you will be calling strlen over and over again, each time, when you really only need to call it once. This is a pretty big inefficiency. You could write:
Code:void encrypt( char e[] )
{
int i;
int len = strlen(e);
for (i = 0; i < len; i++ ) {
e[i] += 1;
}
}
I assume you know what's wrong with this.Quote:
Code:// decrypt data
void decrypt( char *ePtr )
{
ePtr [0] -= 1;
ePtr [1] -= 1;
ePtr [2] -= 1;
ePtr [3] -= 1;
ePtr [4] -= 1;
ePtr [5] -= 1;
ePtr [6] -= 1;
ePtr [7] -= 1;
ePtr [8] -= 1;
ePtr [9] -= 1;
ePtr [10] -= 1;
ePtr [11] -= 1;
ePtr [12] -= 1;
ePtr [13] -= 1;
ePtr [14] -= 1;
ePtr [15] -= 1;
ePtr [16] -= 1;
} // end function decrypt