Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- public class CaesarCipher
- {
- // Encrypts text using a shift od s
- public static StringBuilder encrypt(String text, int s)
- {
- StringBuilder result = new StringBuilder();
- for (int i = 0; i < text.Length; i++)
- {
- if (char.IsUpper(text[i]))
- {
- char ch = (char)(((int)text[i] +
- s - 65) % 26 + 65);
- result.Append(ch);
- }
- else if (char.IsLower(text[i]))
- {
- char ch = (char)(((int)text[i] +
- s - 97) % 26 + 97);
- result.Append(ch);
- }
- else if (char.IsSeparator(text[i]))
- {
- char ch = (char)(((int)text[i] +
- s - 32) % 26 + 32);
- result.Append(ch);
- }
- else if (char.IsPunctuation(text[i]))
- {
- char ch = (char)(((int)text[i] +
- s - 33) % 26 + 33);
- result.Append(ch);
- }
- else if (char.IsDigit(text[i]))
- {
- char ch = (char)(((int)text[i] +
- s - 48) % 26 + 48);
- result.Append(ch);
- }
- }
- return result;
- }
- // Driver code
- public static void Main(String[] args)
- {
- String text = Console.ReadLine();
- int s = 3;
- Console.WriteLine(encrypt(text, s));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement