Advertisement
Guest User

Caesar Cipher

a guest
Mar 15th, 2020
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. public class CaesarCipher
  5. {
  6. // Encrypts text using a shift od s
  7. public static StringBuilder encrypt(String text, int s)
  8. {
  9. StringBuilder result = new StringBuilder();
  10.  
  11. for (int i = 0; i < text.Length; i++)
  12. {
  13. if (char.IsUpper(text[i]))
  14. {
  15. char ch = (char)(((int)text[i] +
  16. s - 65) % 26 + 65);
  17. result.Append(ch);
  18. }
  19.  
  20.  
  21. else if (char.IsLower(text[i]))
  22. {
  23. char ch = (char)(((int)text[i] +
  24. s - 97) % 26 + 97);
  25. result.Append(ch);
  26. }
  27. else if (char.IsSeparator(text[i]))
  28. {
  29. char ch = (char)(((int)text[i] +
  30. s - 32) % 26 + 32);
  31. result.Append(ch);
  32. }
  33. else if (char.IsPunctuation(text[i]))
  34. {
  35. char ch = (char)(((int)text[i] +
  36. s - 33) % 26 + 33);
  37. result.Append(ch);
  38. }
  39. else if (char.IsDigit(text[i]))
  40. {
  41. char ch = (char)(((int)text[i] +
  42. s - 48) % 26 + 48);
  43. result.Append(ch);
  44. }
  45.  
  46.  
  47. }
  48. return result;
  49. }
  50.  
  51. // Driver code
  52. public static void Main(String[] args)
  53. {
  54. String text = Console.ReadLine();
  55. int s = 3;
  56. Console.WriteLine(encrypt(text, s));
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement