I am a beginner and i made a code in C++ to convert 3 characters to caesar cipher but my problem is that i can print only characters to a - w. How can i print x, y ,z using only if else statement. I have tried else if and many other things but it prints for x,y,z {|} which is +3 i know i need to do -23 but i cant get it to work pls help. This is my code for only to a - w. Edit : i still don’t get how to go from x,y,z to a,b,c if some1 has an code example i would appreciate it cus im trying but it for some reason keeps printing {|} which is ch = ch + 3; and not ch = ch - 23; which is the one i want to do with else if statement
char ch1;
char ch2;
char ch3;
cout << "Insert three characters: " << endl;
cin >> ch1;
cin >> ch2;
cin >> ch3;
if (ch1 >= '97' && ch1 <= '119' || ch2 <= '97' && ch2 <= '119' || ch3 >= '97' && ch3 <= '119') { ch1 = ch1 + 3; ch2 = ch2 + 3; ch3 = ch3 + 3;
cout << "Caesar Cipher: " << ch1 << ch2 << ch3 << endl;
} 10 1 Answer
To wrap your characters around, you need the modulo operator, %. But first you must transform the letter to a zero-based value. After that, you have a number in the range [0, 25] and you can offset it. You then take the modulo to wrap the new value to the same range and finally transform it back to an alphabet character.
Example:
ch1 = 'a' + (ch1 - 'a' + 3) % 26;Notice I'm using a character literal here. You should always avoid hard-coding character values, as that just makes your code more difficult to read and less portable. To test if a value is lowercase, there's a standard library function std::islower (and a corresponding std::isupper) found in the header <cctype>.
Fixing your code up a bit, it becomes:
if (std::islower(ch1)) ch1 = 'a' + (ch1 - 'a' + 3) % 26;
if (std::islower(ch2)) ch2 = 'a' + (ch2 - 'a' + 3) % 26;
if (std::islower(ch3)) ch3 = 'a' + (ch3 - 'a' + 3) % 26;
cout << "Caesar Cipher: " << ch1 << ch2 << ch3 << endl;But look at all the code duplication! What if you want to change the key, or use more or less characters, or change the encoding algorithm completely? That's where a function comes in handy:
char EncodeChar(char c, int key)
{ if (std::islower(c)) c = 'a' + (c - 'a' + key) % 26; return c;
}Now you can use it like this:
const int key = 3;
ch1 = EncodeChar(ch1, key);
ch2 = EncodeChar(ch2, key);
ch3 = EncodeChar(ch3, key);Even better, read your characters into a std::string and encode it in-place. You're no longer restricted to exactly three characters:
void CaesarEncode(std::string& s, int key)
{ for (char& c : s) c = EncodeChar(c, key);
}Call that like this:
std::string text = "Hello, world!";
CaesarEncode(text, 3);
std::cout << text << "\n";Now, this will work for positive offsets, but not negatives. If you need to apply a negative offset, you need to add an additional 26 before modulo which ensures you never go below zero. It's useful to support both, because you probably want to decode the ciphertext at some point. You can use the same function but make the key negative.
One way to approach this is to modify a negative key at the last minute with a recursive call in your CaesarEncode function:
void CaesarEncode(std::string& s, int key)
{ if (key < 0) { CaesarEncode(s, 26 - (-key % 26)); return; } for (char& c : s) c = CaesarEncode(c, key);
}Let's continue. What about capital letters? You can handle those too:
char EncodeChar(char c, int key)
{ if (std::isalpha(c)) { char base = std::islower(c) ? 'a' : 'A'; c = base + (c - base + key) % 26; } return c;
}As you can see, using functions makes life much easier, because you don't need to create a mess when beefing up your encoding method. You just change the core logic in one place.
Rolling this all together, and with a few extra helpers from the standard library, you get a reasonably compact program that can encode and decode:
#include <algorithm>
#include <cctype>
#include <iostream>
#include <iterator>
#include <string>
std::string CaesarEncode(const std::string& s, int key)
{ if (key < 0) { return CaesarEncode(s, 26 - (-key % 26)); } auto fnEncodeChar = [key](unsigned char c) -> unsigned char { if (std::isalpha(c)) { char base = std::islower(c) ? 'a' : 'A'; c = base + (c - base + key) % 26; } return c; }; std::string encoded; std::transform(s.begin(), s.end(), std::back_inserter(encoded), fnEncodeChar); return encoded;
}
std::string CaesarDecode(const std::string& s, int key)
{ return CaesarEncode(s, -key);
}
int main()
{ const int KEY = 3; std::string line; while (std::getline(std::cin, line)) { std::string encoded = CaesarEncode(line, KEY); std::string decoded = CaesarDecode(encoded, KEY); std::cout << "Original : " << line << "\n"; std::cout << "Encoded : " << encoded << "\n"; std::cout << "Decoded : " << decoded << "\n"; }
}Input:
This is a test.Output:
Original : This is a test.
Encoded : Wklv lv d whvw.
Decoded : This is a test.