Anagram and substitution
Anagram:
Anagram is to compare two words by sorting them if equals then it's anagram .
using System; public class Demo {
public static void Main () {
string str1 = "heater"; string str2 = "reheat";
char[] ch1 = str1.ToLower().ToCharArray(); char[] ch2 = str2.ToLower().ToCharArray(); Array.Sort(ch1); Array.Sort(ch2); string val1 = new string(ch1); string val2 = new string(ch2); if (val1 == val2) { Console.WriteLine("Both the strings are Anagrams"); } else { Console.WriteLine("Both the strings are not Anagrams"); } } }
Substitution:
Cipher is an encoding technique where we encrypt given words with some key provided .
using System;
class SubstitutionCipher
{
static void Main()
{
string key = "jfkgotmyvhspcandxlrwebquiz";
string plainText = "the quick brown fox jumps over the lazy dog";
string cipherText = Encrypt(plainText, key);
string decryptedText = Decrypt(cipherText, key);
Console.WriteLine("Plain : {0}", plainText);
Console.WriteLine("Encrypted : {0}", cipherText);
Console.WriteLine("Decrypted : {0}", plainText);
Console.ReadKey();
}
static string Encrypt(string plainText, string key)
{
char[] chars = new char[plainText.Length];
for(int i = 0; i < plainText.Length; i++)
{
if (plainText[i] == ' ')
{
chars[i] = ' ';
}
else
{
int j = plainText[i] - 97;
chars[i] = key[j];
}
}
return new string(chars);
}
static string Decrypt(string cipherText, string key)
{
char[] chars = new char[cipherText.Length];
for(int i = 0; i < cipherText.Length; i++)
{
if (cipherText[i] == ' ')
{
chars[i] = ' ';
}
else
{
int j = key.IndexOf(cipherText[i]) - 97;
chars[i] = (char)j;
}
}
return new string(chars);
}
}
using System;
ReplyDeleteusing System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CipherTechnique //Do not change the namespace name
{
public class Program //Do not change the class name
{
public string CipherTechnique(string input) //Do not change the method signature
{
//Implement the code here
int shift=7;
string alphabet="";
string result="";
foreach(var c in input)
{
if((c>='a' && c<='z')||(c>='A' && c<='Z'))
{
alphabet+=c;
}
else
{
continue;
}
}
for(int i=0;i=0 && alphabet[i]<=64)
{
char ch=(char)(((int)alphabet[i]-shift-0)%26+0);
result+=ch;
}
else if(alphabet[i]>=91 && alphabet[i]<=96)
{
char ch=(char)(((int)alphabet[i]-shift-91)%26+91);
result+=ch;
}
else if(alphabet[i]>=123 && alphabet[i]<=127)
{
char ch=(char)(((int)alphabet[i]-shift-123)%26+123);
result+=ch;
}
else
{
char ch=(char)('a'+(alphabet[i]-'a'-shift+26)%26);
result+=ch;
}
}
if(string.IsNullOrEmpty(result))
{
return "no hidden message";
}
else
{
return result;
}
}
static void Main(string[] args) //Do not change the method signature
{
//Implement the code here
Program p=new Program();
Console.WriteLine("Enter the encrypted text");
string encryptedText=Console.ReadLine();
Console.WriteLine(p.CipherTechnique(encryptedText));
}
}
}