fork download
  1. // A C++ program to illustrate Caesar Cipher Technique
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. // This function receives text and shift and
  6. // returns the encrypted text
  7. string encrypt(string text, int s)
  8. {
  9. string result = "";
  10.  
  11. // traverse text
  12. for (int i=0;i<text.length();i++)
  13. {
  14. // apply transformation to each character
  15. // Encrypt Uppercase letters
  16. if (isupper(text[i]))
  17. result += char(int(text[i]+s-65)%26 +65);
  18.  
  19. // Encrypt Lowercase letters
  20. else
  21. result += char(int(text[i]+s-97)%26 +97);
  22. }
  23.  
  24. // Return the resulting string
  25. return result;
  26. }
  27.  
  28. // Driver program to test the above function
  29. int main()
  30. {
  31. string text="Stay Home, and Stay Safe";
  32. int s = 7;
  33. cout << "Text : " << text;
  34. cout << "\nShift: " << s;
  35. cout << "\nCipher: " << encrypt(text, s);
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0s 4492KB
stdin
Standard input is empty
stdout
Text : Stay Home, and Stay Safe
Shift: 7
Cipher: Zahf[OvtlM[huk[Zahf[Zhml