Monday, August 17, 2015

Hackerrank : Caeser Cipher in C#

//caeser cipher
int n = Convert.ToInt32(Console.ReadLine());

string s = Console.ReadLine();

int offset = Convert.ToInt32(Console.ReadLine());

char[] final = new char[n];

for (int i = 0; i < n; i++)



{
//bool ischar = false;

bool isSmall = false;

int curchar = (int)s[i];

if(curchar >= 97 && curchar <= 122)



{
isSmall = true;



curchar = curchar - 32;

}
if (curchar >= 65 && curchar <= 90)



{
int newChar = curchar + offset;

while(newChar >90)


{ newChar = newChar - 26; }
final[i] = isSmall ? (char)(newChar + 32) : (char)(newChar);

Console.Write(final[i]);



}
else



{

final[i] = s[i];
Console.Write(final[i]);



}

}
Console.ReadLine();

No comments:

Post a Comment