forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaesar_Cipher_Encode.cpp
40 lines (31 loc) · 1003 Bytes
/
Caesar_Cipher_Encode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
void caesarCipher(std::string& text, int shift);
int main() {
std::string text;
int shift;
// Input
std::cout << "Enter the text to be encoded: ";
std::getline(std::cin, text);
std::cout << "Enter the shift value: ";
std::cin >> shift;
// Function to encode text
caesarCipher(text, shift);
// Output
std::cout << "Encoded text: " << text << std::endl;
return 0;
}
void caesarCipher(std::string& text, int shift) {
for (size_t i = 0; i < text.length(); ++i) {
char currentChar = text[i];
// Check if the character is an uppercase letter
if (std::isupper(currentChar)) {
text[i] = ((currentChar - 'A' + shift) % 26) + 'A';
}
// Check if the character is a lowercase letter
else if (std::islower(currentChar)) {
text[i] = ((currentChar - 'a' + shift) % 26) + 'a';
}
// Leave non-alphabetic characters unchanged
}
}