I have a file that someone already has inputed. I now need to be able to 'encrpyt' it by changing each letter two spaces. So a's would become c's, b's would become d's and so on. How do I do this? My code so far is this. Thanks!
What you want to do is called a "Caesar Cipher" (because Julius used it to encrypt sensitive messages).
Remember that in C and C++ a char is an integer type. So
cout << ('A' +3) << endl;
prints the letter 'D'.
Also remember that a string is basically an array of chars.
The only thing you will have to be careful of is the ends. For example, if you ignore all spaces and punctuation, then only the letters 'A'..'Z' need be considered. If you add 3 to 'Z', you get a letter that is out of range. So you then have to subtract 'A' to get it back in range. Hence:
cout << ('Z' +3 -'A') << endl;
which produces, correctly, 'C'.
You need to decide what is the domain of your function. That is, are changes limited to 'A'..'Z' and 'a'..'z'? Or are they active over the entire printable ASCII set? etc.
Or,
Assign ch to,
if the result of ch+2 would be out of range because it is 'y' or 'Y' or 'z' or 'Z'
Depending on the case,
Wrap around correctly for uppercase
Wrap correctly for lowercase
Otherwise, it is normal, just add 2.
Both the macros toupper and isupper are available in ctype[.h].
I suspect that his domain covers ASCII 32 to 126, and not just A..Z.
In either case, since this is the beginners forum, I'd try to keep the ternary operator out of simple equations. Just add 2. Use an 'if' statement to test to see if the new value is out of range (too big or too small!), and if it is, subtract 'A' or 32 or whatever the left end of the domain is.
(Sorry, it seems I misread the original post and thought the shift was 3 instead of 2 as the OP indicated. Caesar used three...)