fork download
  1. // Ceasar Cipher
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. string toUpperCase(string text) {
  7. for (char &ch : text) {
  8. ch = toupper(ch);
  9. }
  10. return text;
  11. }
  12.  
  13. string encrypt(string text, int key) {
  14. text = toUpperCase(text); // Convert to uppercase
  15. string result = "";
  16. for (char ch : text) {
  17. if (isupper(ch)) {
  18. result += char((ch - 'A' + key) % 26 + 'A');
  19. } else {
  20. result += ch;
  21. }
  22. }
  23. return result;
  24. }
  25.  
  26. string decrypt(string cipher, int key) {
  27. cipher = toUpperCase(cipher); // Convert to uppercase
  28. string result = "";
  29. for (char ch : cipher) {
  30. if (isupper(ch)) {
  31. result += char((ch - 'A' - key + 26) % 26 + 'A');
  32. } else {
  33. result += ch;
  34. }
  35. }
  36. return result;
  37. }
  38.  
  39. int main() {
  40. string name = "AtulMangla"; // lowercase + uppercase mix
  41. int key = 3;
  42.  
  43. string cipherText = encrypt(name, key);
  44. cout << "Cipher Text: " << cipherText << endl;
  45.  
  46. string decodedText = decrypt(cipherText, key);
  47. cout << "Decoded Text: " << decodedText << endl;
  48.  
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Cipher Text: DWXOPDQJOD
Decoded Text: ATULMANGLA